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

June 5th, 2008 at 2:32 pm
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!
June 5th, 2008 at 2:34 pm
sorry, i mispoke, its not just an ArrayCollection, its through the dataProvider, so in reality I guess any dataprovider that a datagrid can utilize
June 5th, 2008 at 2:57 pm
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.
April 13th, 2009 at 6:36 pm
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.
April 14th, 2009 at 11:37 am
Thanks..i found the code snippet useful
May 28th, 2009 at 11:43 am
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
May 28th, 2009 at 11:49 am
ooh yeah, i got it…
I just have to say:
cursor.current.NameOfNextColumn
Sorry for spaming without thinking…
Anyway, appreciate your tip!
May 28th, 2009 at 1:51 pm
Thanks for looking and thanks for your comments!
July 30th, 2009 at 6:19 am
Hi,
Thanks for this post, , It worked for me !
August 9th, 2009 at 8:45 am
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
September 6th, 2009 at 7:42 am
henry still alive? im so sad for that. kilala mo pa ba ako?
January 12th, 2010 at 11:18 am
I have a checkbox on each row.
In the loop how do I code for those selected?
Thanks
January 13th, 2010 at 12:11 am
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,
January 22nd, 2010 at 4:29 pm
@jenue, I believe you want to look at itemRenderer and itemEditor, they allow you to use a custom component for the cell layout.
May 4th, 2010 at 7:18 am
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.
June 16th, 2010 at 2:46 am
Hi,
Thanks for this post, I was spending so many hours to work it around. Finally It worked for me !
August 17th, 2010 at 9:36 am
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!
September 14th, 2011 at 6:36 am
Thnx it helped me a lot
January 6th, 2012 at 6:18 am
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.