2 Replies Latest reply on Jan 13, 2010 7:14 AM by ForestDemon

    Need to replace data in specific cells of DataGrid columns.

    ForestDemon

      This should be simple, but I seem to be wrestling with it.

       

      I have a DataGrid with DataGridColumns defines and the DataGrid has a DatProvider.

       

      All I want to do (through ActionScript) is to manipulate and update specific cells within the DataGrid, er dataGridColumns.

       

      Thanks all...

       

      -

      fd

        • 1. Re: Need to replace data in specific cells of DataGrid columns.
          archemedia Level 4

          You should write code that manipulates the dataProvider, not the dataGrid itself, then the dataGrid will be updated automatically (if you use data binding).

          TIP: try to use a collection (ArrayCollection or XMLCollection) whenever possible. Use the refresh() method to trigger dataBinding.

           

          Here's an example:

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()">
              <mx:Script>
                  <![CDATA[
                      import mx.collections.ArrayCollection;
                     
                      [Bindable]
                      private var dp:ArrayCollection;
                     
                      private function init():void
                      {
                          var arr:Array = [{v1: 1, v2: 2, v3: 3},
                                           {v1: 4, v2: 5, v3: 6},
                                           {v1: 7, v2: 8, v3: 9}];
                          dp = new ArrayCollection();
                          dp.source = arr;
                      }
                     
                      private function handleNewValue():void
                      {
                          dp.source[rowSetter.value - 1]["v" + String(colSetter.value)] = valSetter.value;
                          dp.refresh();
                      }
                  ]]>
              </mx:Script>
              <mx:DataGrid id="dg" width="100%" height="100%" dataProvider="{dp}">
                  <mx:columns>
                      <mx:DataGridColumn headerText="Value 1" dataField="v1"/>
                      <mx:DataGridColumn headerText="Value 2" dataField="v2"/>
                      <mx:DataGridColumn headerText="Value 3" dataField="v3"/>
                  </mx:columns>
              </mx:DataGrid>
             
              <mx:Form>
                  <mx:FormItem label="Row">
                      <mx:NumericStepper id="rowSetter" minimum="1" maximum="3" stepSize="1"/>
                  </mx:FormItem>
                  <mx:FormItem label="Column">
                      <mx:NumericStepper id="colSetter" minimum="1" maximum="3" stepSize="1"/>
                  </mx:FormItem>
                  <mx:FormItem label="Value">
                      <mx:NumericStepper id="valSetter" minimum="1" maximum="99" stepSize="1"/>
                  </mx:FormItem>
                  <mx:FormItem>
                      <mx:Button label="Set" click="handleNewValue()"/>
                  </mx:FormItem>
              </mx:Form>
          </mx:Application>

          • 2. Re: Need to replace data in specific cells of DataGrid columns.
            ForestDemon Level 1

            Yes I'm binding an ArrayCollection to the grid.

            Your answer is direct, to the point and very helpful.

             

            Thank you so much...

             

            fd