12 Replies Latest reply on May 8, 2007 5:59 AM by rajeeva_nagaraj

    Data provider for datagrid from function

    rajeeva_nagaraj Level 1
      Hi,

      I am having problem in setting the data provider for a datagrid

      I have a datagrid and a button on click of button i am calling a function and in that function i am setting the dataprovider but its not happening

      plz send me some example

      thanks in advance
        • 1. Re: Data provider for datagrid from function
          Rahul Mainkar
          Hi Rajeev,
          You can try out something like this.

          <mx:DataGrid x="262" y="22" id="dg" >
          .
          .
          .
          </DataGrid>
          <mx:Button click="setFunction()"/>

          And the function is

          public function setFunction():void{
          dg.dataProvider=myData;
          }

          I hope this may help you
          • 2. Re: Data provider for datagrid from function
            rajeeva_nagaraj Level 1
            Hi,

            I have generated dynamic controls thru repeaters

            In my application i have a tab navigator control in which tabs has to be generated dynamically along with a datagrid in each tab which are getting generated dynamically, but the problem now is i have show different results on each datagrid and for that i have written seperate http services for that but i am unable to set that services to datagrid

            pls find me some solution
            • 3. Re: Data provider for datagrid from function
              Rahul Mainkar Level 1
              Rajeev,
              You must be using httpService to collect data from a remote location right?
              now you will have to store this data provided by httpService into a xmlListCollection or ArrayCollection and then set it to the dataProvider.

              Have I given you enough hint to work on?
              • 4. Re: Data provider for datagrid from function
                rajeeva_nagaraj Level 1
                Rahul,

                Its puzzling me,

                ok tell me how do i write xml or array collection from httpservice

                I have having my xml data from aspx in the below fashion

                <newdataset>
                <table1>
                <id> 1 </id>
                <name> A </name>
                <id> 2 </id>
                <name> B </name>
                <table1>
                <table2>
                <id> 3 </id>
                <name> C </name>
                <id> 4 </id>
                <name> D </name>
                </table2>
                </newdataset>

                i have generated dynamic datagrids using repeaters and i have 2 datagrids which are generated dynamically
                now i have to fill the contents of <table1> in datagrid1 and <table2> into datagrid2
                how do i do that

                if i use xml or array list how will it be useful for me
                • 5. Re: Data provider for datagrid from function
                  Rahul Mainkar Level 1
                  I ll modify your XML a bit. your xml must have set of tables.

                  <newdataset>
                  <table>
                  <id> 1 </id>
                  <name> A </name>
                  <id> 2 </id>
                  <name> B </name>
                  </table>
                  <table>
                  <id> 3 </id>
                  <name> C </name>
                  <id> 4 </id>
                  <name> D </name>
                  </table2>
                  </newdataset>


                  Now you can collect the 1st table entry as follows

                  var xmlcoll:XMLListcollection=new XmlListCollection();
                  xmlColl=new XMLListCollection(newdataset[0]); // This will give you data for 1st table

                  now set your DataGrid's dataprovider as xmlColl.
                  I havent tested this as I am at home and I dont have flex builder ready available here.
                  I hope this may workout.
                  • 6. Re: Data provider for datagrid from function
                    rajeeva_nagaraj Level 1
                    Hi Rahul,

                    I have another doubt, its related to combobox

                    i am using http service to get the data from aspx

                    aspx returns me the data in the below fashion
                    <NewDataSet>
                    <Table>
                    <Designation> CEO </Designation>
                    <Designation> Project Manager </Designation>
                    <Designation> Project Leader </Designation>
                    .
                    .
                    .
                    </Table>
                    </NewDataSet>
                    now i want to add all those designations to my combobox but its not happening there

                    can you give me some idea for that

                    thanks in advance

                    regards
                    Rajeev
                    • 7. Re: Data provider for datagrid from function
                      Rahul Mainkar Level 1
                      Hello Raajev,
                      Hey sorry for late reply was out on vacation.
                      Its real simple.
                      Here is your code

                      <?xml version="1.0" encoding="utf-8"?>
                      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute"
                      initialize="iniApp()">
                      <mx:Script>
                      <![CDATA[
                      import mx.controls.Alert;
                      import mx.collections.XMLListCollection;
                      public var result:XMLListCollection;

                      public function iniApp():void{
                      result =new XMLListCollection(data.Table.Designation);
                      cb.dataProvider=result;
                      Alert.show(result.toString());
                      }
                      ]]>
                      </mx:Script>
                      <mx:XML id="data" xmlns="">
                      <NewDataSet>
                      <Table>
                      <Designation>CEO</Designation>
                      <Designation>Project_Manager</Designation>
                      <Designation>Project_Leader</Designation>
                      </Table>
                      </NewDataSet>
                      </mx:XML>
                      <mx:ComboBox id="cb" x="110" y="202" labelField="Designation" width="211"></mx:ComboBox>
                      </mx:Application>
                      • 8. Re: Data provider for datagrid from function
                        rajeeva_nagaraj Level 1
                        Hi Rahul,

                        I have used a repeater control to generate dynamic tabs in a tab navigator and in each tab i am generating few labels, now i want to build an array which should loop thru the repeater and pick all those label values from all those dynamically generated tabs, and i want to bind that array to a datagrid

                        how can i do this in flex, plz find me some solution

                        Regards
                        Rajeev

                        • 9. Re: Data provider for datagrid from function
                          cosmacol
                          Hi rejeeva,

                          when you put a component inside an mx:Repeater tag, an array of components will be created.. you could simply iterate over it using a for loop.

                          Note also that multiple nested mx:Repeater tags will generate an array of arrays..

                          you should read the "Referencing repeated components" section of the flex documentation:

                          http://livedocs.adobe.com/flex/201/html/repeater_078_07.html

                          Bye
                          Cosma
                          • 10. Re: Data provider for datagrid from function
                            Rahul Mainkar Level 1
                            Hi Raajev,
                            This may give you a direction to think.

                            <?xml version="1.0" encoding="utf-8"?>
                            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute"
                            >
                            <mx:Script>
                            <![CDATA[
                            import mx.collections.ArrayCollection;
                            import mx.controls.Alert;
                            import mx.collections.XMLListCollection;
                            public var result:XMLListCollection;
                            [Bindable]
                            public var arr:Array=[1,2,3,4,5];
                            [Bindable]
                            public var res:ArrayCollection=new ArrayCollection();
                            [Bindable]
                            public var num:int=100;
                            public function callFunc():void{
                            for(var i:int=0;i<arr.length;i++)
                            {
                            Alert.show((Number(res.toString())+i).toString());

                            }
                            }


                            ]]>
                            </mx:Script>

                            <mx:TabNavigator x="187" y="99" width="200" height="200">
                            <mx:Repeater id="rep" dataProvider="{arr}" >
                            <mx:Canvas label="Tab 1" width="100%" height="100%">
                            <mx:Label x="10" y="36" text="{num.toString()}" initialize="res.addItem(num);"/>
                            </mx:Canvas>
                            </mx:Repeater>
                            </mx:TabNavigator>
                            <mx:Button x="231" y="367" label="Button" click="callFunc()"/>
                            </mx:Application>
                            • 11. Re: Data provider for datagrid from function
                              rajeeva_nagaraj Level 1
                              Hi Everybody,

                              Somehow i am able to write an array and that array i am binding to a datagrid,
                              but the problem now is my array is getting overwritten on the previous values when i loop
                              how do i preserve the old values on array

                              plz help me

                              • 12. Re: Data provider for datagrid from function
                                rajeeva_nagaraj Level 1
                                Hi Rahul

                                I have a datagrid and a combobox in one of the column so i am using item render for that but i am unable to populate data dynamically into that rendered combobox
                                how can i do that, plz find me some solution

                                Regards
                                Rajeev