3 Replies Latest reply on Jul 5, 2006 11:47 PM by peacexyz

    retrieve data from datagrid

    peacexyz

      Hi all

      I am using flex 2.0 beta 3. I am facing a problem in retrieving data from a dataGrid which i populate by drag and drop from another dataGrid. Once user finishes drag and drops he chooses to save the dropped data in the dataGrid. How can i retrieve the data in the dataGrid to an Array.

      Rakesh

        • 1. Re: retrieve data from datagrid
          ur_dtrain Level 1
          var newData:ArrayCollection = new ArrayCollection(myDG.dataProvider);
          • 2. Re: retrieve data from datagrid
            peacexyz Level 1
            Hi

            I tried what you suggested. The code i tried is given below.

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
            <mx:DataGrid x="248" y="28" width="209" dataProvider="{targetData}" id="targetDG">
            <mx:columns>
            <mx:DataGridColumn headerText="Name" dataField="name"/>
            <mx:DataGridColumn headerText="Id" dataField="id"/>
            <mx:DataGridColumn headerText="Role" dataField="role"/>
            </mx:columns>
            </mx:DataGrid>
            <mx:DataGrid x="10" y="28" width="209" dataProvider="{sourceData}" id="sourceDG">
            <mx:columns>
            <mx:DataGridColumn headerText="Name" dataField="name"/>
            <mx:DataGridColumn headerText="Id" dataField="id"/>
            <mx:DataGridColumn headerText="Role" dataField="role"/>
            </mx:columns>
            </mx:DataGrid>

            <mx:Script>
            <![CDATA[
            import mx.collections.ArrayCollection;

            [Bindable]
            public var sourceData:ArrayCollection = new ArrayCollection (
            [{name:"magie1", id:"1234", role:"xyz1"},
            {name:"magie2", id:"1235", role:"xyz2"},
            {name:"magie3", id:"1236", role:"xyz3"},
            {name:"magie4", id:"1237", role:"xyz4"},
            {name:"magie5", id:"1238", role:"xyz5"}]);

            [Bindable]
            public var targetData:ArrayCollection = new ArrayCollection(sourceDG.dataProvider);
            //var newData:ArrayCollection = new ArrayCollection(myDG.dataProvider);
            ]]>
            </mx:Script>
            </mx:Application>

            It gives the following compilation error:

            Implicit coercion of a value with static type Object to a possibly unrelated type Array. | dataGridTest.mxml | dataGridTest line 31 June 30, 2006 12:53:10 PM 2185
            Text Text

            Can you please help me to fix the problem.

            Rakesh
            • 3. Re: retrieve data from datagrid
              peacexyz Level 1
              Hi all

              here is the modified version of above code to retrieve data from a dataGrid ...

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">

              <mx:DataGrid x="10" y="28" width="209" dataProvider="{sourceData}" id="sourceDG">
              <mx:columns>
              <mx:DataGridColumn headerText="Name" dataField="name"/>
              <mx:DataGridColumn headerText="Id" dataField="id"/>
              <mx:DataGridColumn headerText="Role" dataField="role"/>
              </mx:columns>
              </mx:DataGrid>

              <mx:Script>
              <![CDATA[
              import mx.collections.ArrayCollection;

              [Bindable]
              public var sourceData:Array = [{name:"magie1", id:"1234", role:"xyz1"},
              {name:"magie2", id:"1235", role:"xyz2"},
              {name:"magie4", id:"1237", role:"xyz4"},
              {name:"magie5", id:"1238", role:"xyz5"}];

              [Bindable]
              public var targetData:Object;

              public function load() : void {
              targetData = sourceDG.dataProvider;
              }
              ]]>
              </mx:Script>

              <mx:DataGrid x="248" y="28" width="209" dataProvider="{targetData}" id="targetDG">
              <mx:columns>
              <mx:DataGridColumn headerText="Name" dataField="name"/>
              <mx:DataGridColumn headerText="Id" dataField="id"/>
              <mx:DataGridColumn headerText="Role" dataField="role"/>
              </mx:columns>
              </mx:DataGrid>
              <mx:Button x="201" y="237" click="load()" label="Load"/>
              </mx:Application>

              -Rakesh