6 Replies Latest reply on Jan 4, 2007 12:29 PM by 187_2007

    DataGrid and dataprovider question

    187_2007 Level 1
      Is it possible to have 2 or 3 DataGridColumns that display different data? Cause I cannot bind the different data to the DataGridColumns only the DataGrid parent. Here is what I have that works:

      <mx:DataGrid x="79" y="124" id="myGrid" dataProvider="{qry_result}" height="142">
      <mx:columns>
      <mx:DataGridColumn headerText="Names" dataField="col1"/>
      </mx:columns>
      </mx:DataGrid>
      <mx:DataGrid x="180" y="124" height="142" id="myGrid2" dataProvider="{cowz.FatCow()}">
      <mx:columns>
      <mx:DataGridColumn headerText="Addresses" dataField="col10"/>
      </mx:columns>
      </mx:DataGrid>

      But this is not possible..... Why? What's the solution here?

      <mx:DataGrid x="79" y="124" id="myGrid" height="142">
      <mx:columns>
      <mx:DataGridColumn headerText="Names" dataField="col1" dataProvider="{qry_result}" />
      <mx:DataGridColumn headerText="Addresses" dataField="col10" dataProvider="{cowz.FatCow()}">
      </mx:columns>
      </mx:DataGrid>
        • 1. Re: DataGrid and dataprovider question
          187_2007 Level 1
          suggestions?
          • 2. Re: DataGrid and dataprovider question
            dimival Level 1
            I don't think that's possible, but you can place two datagrids next to each one

            <mx:HBox>
            <mx:DataGrid dataprovider="{qry_result}"/>
            <mx:DataGrid dataprovider="{cowz.FatCow()}"/>
            </mx:HBox>

            Another solution would be to create a dataprovider merging your two providers and then pass that one to the datagrid, datagridcolumns can display a specific property from the objects conatined in the dataprovider, so you will have to create an object that contained both of the information that you want to display
            • 3. Re: DataGrid and dataprovider question
              ntsiii Level 3
              I'd go with the second suggestion, creating an ArrayCollection of objects.

              Tracy
              • 4. Re: DataGrid and dataprovider question
                Galper
                I have a similar question on this topic. I am accessing DB via Hibernate framework.
                I have User table with CompanyID Foreign Key that refers to the Company table.
                I am trying to display a list of users and companies where they work in the same datagrid.
                I can get the user name to appear, but no matter what I do I can't get the Company name to show up in the datagrid.

                I am using lazy="false" and I am sure that the company information is pulled from DB, because I can access it via user.company.name when I click on a user row in the datagrid.

                Here is the code that I currently have in my .mxml file:

                ...
                ds.fill(objectList, "flex:hql", "From User");
                ...
                <mx:ArrayCollection id="objectList" />
                <mx:DataService id="ds" destination="user"/>
                <mx:DataGrid id="dg" dataProvider="{objectList}" width="100%" height="100%">
                <mx:columns>
                <mx:DataGridColumn dataField="lastName" headerText="Last Name"/>
                <mx:DataGridColumn dataField="company.name" headerText="Company"/>
                </mx:columns>
                </mx:DataGrid>

                Please, help!
                Thanks,
                Galper
                • 5. Re: DataGrid and dataprovider question
                  Galper Level 1
                  I figured it out, you have to use labelFunction parm of DataGridColumn element:
                  ...
                  ds.fill(objectList, "flex:hql", "From User");
                  ...
                  protected function getCompanyName(row:Object, column:DataGridColumn):String {
                  return row.company.name;
                  }
                  ...
                  <mx:ArrayCollection id="objectList" />
                  <mx:DataService id="ds" destination="user"/>
                  <mx:DataGrid id="dg" dataProvider="{objectList}" width="100%" height="100%">
                  <mx:columns>
                  <mx:DataGridColumn dataField="lastName" headerText="Last Name"/>
                  <mx:DataGridColumn dataField="companyName" labelFunction="getCompanyName" headerText="Company"/>
                  </mx:columns>
                  </mx:DataGrid>
                  • 6. Re: DataGrid and dataprovider question
                    187_2007 Level 1
                    I see. That looks like a good way also. I solved it another way. First I made a class:

                    package valueObjects
                    {
                    [Bindable]
                    public class Grid
                    {
                    public var NAME:String;
                    public var AGE:Number;
                    public var HEIGHT:String;
                    public var WEIGHT:String;
                    public var SEX:String;
                    public var LASTNAME:String;


                    public function Grid (_name:String, _age:Number, _height:String, _weight:String, _sex:String, _lastname:String)
                    {
                    NAME = _name;
                    AGE = _age;
                    HEIGHT = _height;
                    WEIGHT = _weight;
                    SEX = _sex;
                    LASTNAME = _lastname;
                    }


                    public static function buildemployee(o:Object):Grid
                    {
                    var e:Grid = new Grid(o.NAME, o.AGE, o.HEIGHT, o.WEIGHT, o.SEX, o.LASTNAME);
                    return e;
                    }

                    }
                    }

                    Then I Binded the class

                    [Bindable]
                    private var mygrid:Grid;


                    THEN in my function I did this:
                    public function resultHandler1(event:ResultEvent):void
                    {
                    var myStruct:Object = event.result;
                    this.mygrid = Grid.buildemployee(myStruct);
                    }


                    What does all of this do?
                    1: better error handling due to the class. Also, the cool feature is I can do this in my GRID


                    <mx:DataGrid x="322" y="124" dataProvider="{mygrid}">
                    <mx:columns>
                    <mx:DataGridColumn headerText="Name" dataField="NAME" />
                    <mx:DataGridColumn headerText="Age" dataField="AGE" />
                    <mx:DataGridColumn headerText="Last Name" dataField="LASTNAME"/>
                    </mx:columns>
                    </mx:DataGrid>

                    I hope this helps someone :)