Looping Through Datagrid Rows in Flex

It wasn't readilly apparent to me how to loop through some datagrid rows in Flex. Finally, I came across Abdul Qabiz's DataGridDataExporter class, which basically does just that.

The key is to set up a cursor that will loop through the Data Provider for the datagrid.

Something like this:

ACTIONSCRIPT:
  1. var dp:Object=MyDG.dataProvider;
  2. var cursor:IViewCursor=dp.createCursor();
  3. while( !cursor.afterLast )
  4. {
  5.   // Access each column field like: cursor.current.MyFieldName
  6.   trace(cursor.current.MyFieldName);
  7.   // Obviously don't forget to move to next row:
  8.   cursor.moveNext();
  9. }

That's all there is to it.

19 Responses to “Looping Through Datagrid Rows in Flex”

  1. TJ Downes Says:

    Hey Josh, more accurately, looping through the rows of an ArrayCollection. This would also apply to any component utilizing an array collection as the dataProvider.

    Im assuming, because I haven’t used it yet, that you can probably also use the cursor to loop through other collections, like xmlListCollections.

    Thanks for the tip, we will be playing around with this to see what the performance is like compared to a for each loop!

  2. TJ Downes Says:

    sorry, i mispoke, its not just an ArrayCollection, its through the dataProvider, so in reality I guess any dataprovider that a datagrid can utilize

  3. Josh Says:

    TJ, thanks for your comments. My initial pause when trying to figure out how to loop thru the datagrid was because I was thinking, “Why can’t I just access the ArrayCollection of the datagrid?” But by using this method, I can loop thru the datagrid regardless of how it gets the data.

  4. Wiskers Says:

    Thanks I found this to be exactly what I needed. I spent the last hour trying to pull field values out of the datagrid not the dataprovider.

  5. Anenth Says:

    Thanks..i found the code snippet useful

  6. Dan Says:

    Thanks, that was really helpful.Exactly what I needed.
    Can you tell us, how to jump/move to the next column?

    Let’s assume I’m looking for an email address.
    After it was found, i want to move to the next column in that specific row where the email was found.

    Like:
    while( !cursor.afterLast )
    {
    // Access each column field like: cursor.current.MyFieldName
    trace(cursor.current.Email);
    if(cursor.current.Email == email)
    {
    trace(“email found”);
    // go to NEXT COLUMN HERE AND READ IN THAT VALUE

    }

    thanks a lot, again ;)

  7. Dan Says:

    ooh yeah, i got it…

    I just have to say:

    cursor.current.NameOfNextColumn

    Sorry for spaming without thinking…
    Anyway, appreciate your tip!

  8. Josh Says:

    Thanks for looking and thanks for your comments!

  9. Santosh Says:

    Hi,
    Thanks for this post, , It worked for me !

  10. Henry Mempin Says:

    How can i parse the data if cursor.current.MyFieldName (while field name is in numeric form), i read excel file thru ByteArray and transpose it to ArrayCollection and when i display the data, the datagrid header listed in a numeric form (eg, 0, 1, 2, 3, 5), is there any possibilities to convert the numeric form into Object?

    Thank you very much

  11. Owen Says:

    henry still alive? im so sad for that. kilala mo pa ba ako?

  12. KMull Says:

    I have a checkbox on each row.
    In the loop how do I code for those selected?

    Thanks

  13. jenue Says:

    i want to loop values in datagrid and in that specific cell, i want to renderer a ComboBox… Is it possible? If possible, how do we do that?

    thanks,

  14. JB Says:

    @jenue, I believe you want to look at itemRenderer and itemEditor, they allow you to use a custom component for the cell layout.

  15. Sri Says:

    I tried the same way for Advanced DataGrid. It worked fine but, When I open any of parent nodes, I see duplicate values.
    Any way to traverse through the Advanced DataGrid properly while having open nodes as well.

  16. Kannan Says:

    Hi,
    Thanks for this post, I was spending so many hours to work it around. Finally It worked for me !

  17. NewToFlex Says:

    Hey,

    I have an ADG where I have a checkbox (from a custom renderer) in one of the columns for all the child rows. I need to find out which boxes are checked. How do I access the selected property of the checkbox in AS?

    Thanks a ton in advance!

  18. Krishh Says:

    Thnx it helped me a lot

  19. Deepak Says:

    Hi Nice peace of code it helped me alot.
    I have one question like if we apply labelfunction for any column in that Datagrid then that value won’t be available in dataprovider,in this type of situation what can we do.I need the data which is presented in screen(Datagrid) as it is pls give me some idea and small example also.

    Thanks in advance.

Leave a Reply

  Theme Brought to you by Directory Journal and Elegant Directory.