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:

var dp:Object=MyDG.dataProvider;
var cursor:IViewCursor=dp.createCursor();
while( !cursor.afterLast )
{
  // Access each column field like: cursor.current.MyFieldName
  trace(cursor.current.MyFieldName);
  // Obviously don't forget to move to next row:
  cursor.moveNext();
}

That's all there is to it.

3 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.

Leave a Reply

  Theme Brought to you by Directory Journal and Elegant Directory.