2 Replies Latest reply on Jul 28, 2010 11:02 PM by BhaskerChari

    Dynamic text in a dataGrid column?

    peteandrus Level 1

      i have a datagrid. i want to populate the first column with a different label depending on the row number.

       

      A good example of this would be

       

      Column 1 label

      Label 1

      Label 2

      Label 3

      Label 4

       

      I have these labels in an array, so i could access them if i had the row number.

       

      Anyone know how to do this?

       

      thanks!

        • 1. Re: Dynamic text in a dataGrid column?
          VRPDeveloper Level 3

          Your question is not clear...could u plz give proper example...what do u want ...

          Do you want to find row number?


          • 2. Re: Dynamic text in a dataGrid column?
            BhaskerChari Level 4

            Hi peteandrus,

             

            You can simply use the code snippet below to display the labels row wise....

             

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
               <mx:Script>
               <![CDATA[
                import mx.controls.Alert;
                 
                  [Bindable]private var myAC:Array = [
                     {Column1:'Label 1', Column2:"Text 1"},
                     {Column1:'Label 2', Column2:"Text 2"},
                     {Column1:'Label 3', Column2:"Text 3"},
                     {Column1:'Label 4', Column2:"Text 4"}
                     ];
                 
               ]]>
               </mx:Script>

            <mx:DataGrid id="dataGrid" dataProvider="{myAC}" x="10" y="177" visible="true">          
              <mx:columns>
               <mx:DataGridColumn  headerText="Column 1 label" dataField="Column1"/>           
               <mx:DataGridColumn dataField="Column2" headerText="Column 2 label" />           
              </mx:columns>
            </mx:DataGrid>
            </mx:Application>

             

             

            If you really want to find the row index based assignment you can try something using labelFunction for the DataGrid column...

             

             

            Something like below:

             

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
               <mx:Script>
               <![CDATA[
                import mx.controls.dataGridClasses.DataGridColumn;
                import mx.controls.Alert;
                 
                [Bindable]private var myAC:Array = [
              {Column1:'Label 1', Column2:"Text 1"},
                    {Column1:'Label 2', Column2:"Text 2"},
                    {Column1:'Label 3', Column2:"Text 3"},
                    {Column1:'Label 4', Column2:"Text 4"}
                    ];

            private var counter:int=-1;         
                private function column1LabelFunction(item:Object, coulmn:DataGridColumn):String
                {
                 var strLabel:String="";
                 counter++;
                 if(counter < myAC.length)
                 {
                  strLabel = myAC[counter].Column1;
                 }
              return strLabel;    
                }
                 
               ]]>
               </mx:Script>

            <mx:DataGrid id="dataGrid" dataProvider="{myAC}" x="10" y="177" visible="true">          
              <mx:columns>
               <mx:DataGridColumn  headerText="Column 1 label" labelFunction="column1LabelFunction"/>           
               <mx:DataGridColumn dataField="Column2" headerText="Column 2 label" />           
              </mx:columns>
            </mx:DataGrid>
            </mx:Application>

             

             

            Thanks,

            Bhasker Chari