Skip navigation
Currently Being Moderated

In DataGrid, how do I count the number of selected CheckBox?

Jan 28, 2013 11:06 AM

Tags: #actionscript #itemrenderer #datagrid #checkbox

I am using a CheckBox as an itemRenderer in a DataGrid but am not certain how I can count the number of items selected.  Below is my sample code.


<?xml version="1.0"?>

<!-- dpcontrols/DataGridPassData.mxml -->

<mx:Application xmlns:mx=""




                              import mx.collections.*;




                              public var dpTests:ArrayCollection;



                              private var vCountSelected:int=0;


                              private var DGArray:Array = [

                                        {Category:'Transfer', Status:'Active', Price:10.55},

                                        {Category:'Transfer', Status:'Inactive', Price:0},

                                        {Category:'Transfer', Status:'Archived', Price:0},

                                        {Category:'Initial', Status:'New', Price:25.75},

                                        {Category:'Initial', Status:'Old', Price:15.25}];



                              //Initialize dpTests ArrayCollection variable from the Array.

                              public function initData():void {

                                        dpTests=new ArrayCollection(DGArray);



                              protected function dgTests_changeHandler(event:ListEvent):void


                                        var count:int=0;


                                        // loop through the  datagrid

                                        for (var s:int=0; s < dgTests.dataProvider.length; s++)


                                                  // Select a row during each loop



                                                  //var c:int=dgTests.selectedItem.dfSelected;

                                                  var b:Boolean=dgTests.selectedItem.dfSelected;


                                                  if (b=true)


                                                            // increment the variable




                                        // assign the local variable to the private variable




                              private function itemClickEvent(event:ListEvent):void {








          <mx:DataGrid id="dgTests" width="350" height="200" change="dgTests_changeHandler(event)"

                                         dataProvider="{dpTests}" itemClick="itemClickEvent(event)">


                              <mx:DataGridColumn dataField="Category" />

                              <mx:DataGridColumn dataField="Status" />

                              <mx:DataGridColumn dataField="Price" />

                              <mx:DataGridColumn id="dgcCategorySelected"











          <mx:Form id="fSumm">

                    <mx:FormItem label="Column Index:">

                              <mx:Label id="clickColumn"/>               


                    <mx:FormItem label="Row Index:">

                              <mx:Label id="clickRow"/>              


                    <mx:FormItem label="Type:">

                              <mx:Label id="eventType"/>             


                    <mx:FormItem label="Count Selected:">

                              <mx:Label id="lCountSelected" text="{this.vCountSelected}"/>             





My assumption (which is obviously wrong) is that the value for count should increment and decrement as items are checked and unchecked.  Instead, when I check the first checkbox I get the total of items in the dataprovider.


How can I just get the count of the items selected with the checkbox?




  • Currently Being Moderated
    Jan 28, 2013 7:49 PM   in reply to lee704

    Not sure, but your code looks like

        if (b = true)


    and maybe should be


        if (b == true)


    or just


        if (b)

    Mark as:
  • Currently Being Moderated
    Jan 29, 2013 1:21 PM   in reply to lee704

    What does your code look like now?

    Mark as:
  • Currently Being Moderated
    Jan 30, 2013 9:12 AM   in reply to lee704

    Try initializing that field to false in each array item.



    If that doesn’t work, try an actual class definition instead of plain objects.  The DG is trying to guess the datatype of the dfSelected field in order to know how to save the value.  If it guesses wrong, the way booleans get handled could cause them to appear as if they are true.

    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points