11 Replies Latest reply on Feb 1, 2010 3:05 PM by Flex harUI

    Displaying Row Number in DataGrid

    olegkon Level 1

      Hi,

       

      Is there an easy way to display a Row Number (in the very first column) in DataGrid or AdvancedDataGrid ?

       

      I am trying to emulate Excel kind of functionality in DataGrid,

      so need to Open/Load & Save to file.

       

      I do understand that it is not difficult to do,

      but do not want to Load & Save these Row Numbers to file.

       

      Is there any easy standard solution for that ?

       

      Any code samples ?

       

       

      TIA,

      Oleg.

        • 1. Re: Displaying Row Number in DataGrid
          Matt Le Fevre Level 4

          datagrid row number is fairly straightforward, or so google has led me to believe

           

          http://www.cflex.net/showFileDetails.cfm?ObjectID=735

          • 2. Re: Displaying Row Number in DataGrid
            olegkon Level 1

            Grizzz,

             

            Maybe I didn't express that enough.

             

            I mean, is there any feature in DataGrid (or maybe in AdvancedDG)

            which lets you make RowNumber column external ro data ArrayCollection,

            part of the GUI rather than part of Data ?

             

            (so I don't need to Load/Save it in Excel file)

             

             

            Thanks,

            Oleg.

            • 3. Re: Displaying Row Number in DataGrid
              Flex harUI Adobe Employee

              If you don't have a lot of rows, you can use labelFunction on a column that

              calls getItemIndex on the data.  It does a linear search which is why it

              will slow you down if you have a lot of rows.

               

              If you have a lot of rows, you might try a custom renderer that displays the

              value of listData.rowIndex + dataGrid.verticalScrollPosition.

              • 4. Re: Displaying Row Number in DataGrid
                olegkon Level 1

                I like that labelFunction, tried to use it with DataGrid.

                It worked fine in one case (when I initialize the DataGrid), returning me correct row numbers.

                 

                But in another case (where I load the data from the file (as XMLListCollection) into the dataGrid:

                I loop through the DG columns, and for column=0 do: 

                dgc.labelFunction = lfRowNum;

                 

                then:  columns.push(dgc);

                 

                dg.dataProvider = myXMLListCollection;

                 

                So in this case I do not loop through rows of the collection.

                And then I see the value 0 as row number for all rows.

                 

                Is there some trick to the labelFunction ?

                 

                TIA,

                Oleg.

                • 5. Re: Displaying Row Number in DataGrid
                  Flex harUI Adobe Employee

                  No real tricks to labelfunction.  But you can't just push a column. You have

                  to modify the array and reassign the columns property.

                  • 6. Re: Displaying Row Number in DataGrid
                    olegkon Level 1

                    I guess, that labelFunction is a callback function.

                    It is getting called, but in all rows it's equal 0.

                     

                    > You have to modify the array and reassign the columns property.

                    I do not need to modify that ArrayCollection (maybe add a blank string to its elements?), 

                    and I am assigning it to the DataGrid.dataProvider.

                    Is that sufficient ?

                    • 7. Re: Displaying Row Number in DataGrid
                      Flex harUI Adobe Employee

                      Are you getting a column of zeros?  If so, then you added the column

                      correctly.  Maybe show us what you are using for a labelFunction.

                      1 person found this helpful
                      • 8. Re: Displaying Row Number in DataGrid
                        olegkon Level 1

                        Yes, I am getting column with all "0".

                         

                        I took labelFunction from the link in one of the previous posts as-is:

                         

                        // labelFunction for Row Number of the DataGrid

                         

                         

                        private function lfRowNum(oItem:Object, iCol:int):String {

                         

                        var iIndex:int = gridData.getItemIndex(oItem) + 1;

                        Alert.show(

                        "lfRowNum() i="+iCol+", rowNum="+String(iIndex)+", item='"+oItem.toString()+"'");

                         

                        return String(iIndex);

                        }

                         

                        ..........

                        grid.dataProvider = null; // reset

                         

                         

                         

                        var dgc:DataGridColumn;

                         

                        var aColumnsNew:Array = new Array();

                         

                        for (var i:int=0; i <= aColumnDef.length; i++) { //loop over the column definition array (& add 0-th column - row num)

                        dgc = new DataGridColumn(); //instantiate a new DataGridColumn

                         

                         

                        if (i > 0) {

                        oColumnDef = aColumnDef[i-1];

                        dgc.dataField = oColumnDef.dataField;

                        ...

                         

                        }

                         

                        else { //  add 0-th column (row num)

                        dgc.dataField = "";

                         

                        ...

                        dgc.labelFunction = lfRowNum;

                         

                        }

                        aColumnsNew.push(dgc);

                         

                        }

                         

                        Sorry, I could not format that code ptoperly, would not let me remove spaces and blank lines.

                        • 9. Re: Displaying Row Number in DataGrid
                          Flex harUI Adobe Employee

                          I'm not sure what gridData is, but it is acting as if it doesn't contain the

                          data items.

                          • 10. Re: Displaying Row Number in DataGrid
                            olegkon Level 1

                            gridData is an XMLListCollection, assigned to grid.dataProvider:

                            dataProvider={gridData}

                             

                            few lines later I do:

                            grid.dataProvider = _xlcCatalog;   [another XMLListCollection with data read from Excel file]

                             

                             

                            That Alert in  iterated though the data [can see it correctly], what is strange to me that for each row it shows:

                            i [aka iCol] = 0  and  rowNum [aka iIndex]= 0

                            • 11. Re: Displaying Row Number in DataGrid
                              Flex harUI Adobe Employee

                              I would debug into the getItemIndex call and see why it isn't returning the

                              right index.