3 Replies Latest reply on Oct 7, 2009 5:42 AM by lee704

    Dynamically select all rows in a DataGrid

    lee704 Level 1

      Is there a simple way to dynamically select all rows of a DataGrid control?  I'd like to select all the rows as soon as the DataGrid is created.

       

      Thanks!

      Lee

        • 1. Re: Dynamically select all rows in a DataGrid
          Gregory Lafrance Level 6

          This code should answer your question.

           

          If this post answered your question or helped, please mark it as such.

           

          <?xml version="1.0"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
             initialize="initData()" creationComplete="init();">
             <mx:Script>
             <![CDATA[
              import mx.events.ListEvent;
                import mx.collections.*;
                private var DGArray:Array = [
                   {Artist:'Pavement', Album:'Slanted and Enchanted', Price:11.99},
                   {Artist:'Pavement', Album:'Brighten the Corners', Price:11.99}];
                   
                [Bindable] public var initDG:ArrayCollection;
                public function initData():void {
                   initDG=new ArrayCollection(DGArray);
                }
                
                public function init():void{
                 var arr:Array = new Array();
                 for(var a:uint=0;a<myGrid.dataProvider.length;a++){
                   arr.push(a);
                 }
                 myGrid.selectedIndices = arr;
                }
             ]]>
             </mx:Script>
             <mx:DataGrid id="myGrid" width="350" height="200" 
                dataProvider="{initDG}">
                <mx:columns>
                   <mx:DataGridColumn dataField="Artist" />
                   <mx:DataGridColumn dataField="Album" />
                   <mx:DataGridColumn dataField="Price" />
                </mx:columns> 
             </mx:DataGrid>
          </mx:Application>
          
          • 2. Re: Dynamically select all rows in a DataGrid
            lee704 Level 1

            Excellent example Greg.  Thank you!

            • 3. Re: Dynamically select all rows in a DataGrid
              lee704 Level 1

              As a follow-up...

               

              What if I only wanted to select the rows where the rows met a certain criteria like Column B > 0?

               

              For example, I have a DataGrid with 2 columns: Column A and Column B.  The DataGrid had 10 rows of data, but only 5 rows have data that meet the criteria specified (Column B > 0).

               

              Can you provide a suggestion for doing this?

               

              Thanks!