2
votes

What would be the best way to select all items in a datagrid.

I was thinking of doing a for loop and then select each item, but is there a better way for achieving this?

3

3 Answers

5
votes

What type of dataProvider are you using?

If you have an array, you can do this:

dataGrid.selectedItems = dataGrid.dataProvider;

If you have an ArrayCollection you can do this:

dataGrid.selectedItems = dataGrid.dataProvider.source;

For XML, you'll probably have to loop.

More info on the selectedItems property:

http://livedocs.adobe.com/flex/3/langref/mx/controls/listClasses/ListBase.html#selectedItems

4
votes

Hi I also tried all these with mx.dataGrid but doesn't work, Here I fond a good way

var indexArr:Array = [];
for(var i:uint= 0; i<dataGrid.dataProvider.length;i++ )
{

    indexArr.push(i);    

} 
dataGrid.selectedIndices= indexArr;

this works for me

2
votes

I'll also add the complementary of this question. To unselect all items of a datagrid, the answer is not

dataGrid.selectedItems = null;

But we'll be

dataGrid.selectedItems = new Array;

The first will throw NullPointerExceptions.