5 Replies Latest reply on Jan 12, 2007 8:48 AM by Pigg_Man

    Modify items in datagrid

    Pigg_Man Level 1
      I have a datagrid that is bound to an array collection. Is there a simpler way to modify the properties in the datagrid and have the datagrid change to reflect the changes.. This is my current code to alter on property and get the datagrid display to update itself...

      var item:Object = DGCollection.getItemAt (0);
      item.label= "changed text";

      This code will change the name of a row and update the datagrid to display the changed text. I always thought that you could use a one liner to do this same thing. Any ideas? or is this how you do it
        • 1. Re: Modify items in datagrid
          FlightGuy Level 1
          My datagrids update automatically as I modify the content - the elements of the collection are instances of a class that has all its properties set [Bindable]. Give us a bit more of an example of where the data comes from.

          • 2. Re: Modify items in datagrid
            peterent Level 2
            In Flex 1.5 it took less lines of code. The Array class was given additional functions like addItem and editField. In Flex 2.0 we took a more traditional approach and created a separate Collection classes to be used as the dataProvider for List classes.

            We also wanted to make the system faster. Calling itemUpdated once you are through updating your items reduces the number of data-binding events that get triggered.
            • 3. Re: Modify items in datagrid
              Pigg_Man Level 1
              This is what I have:
              private var DGCollection:ArrayCollection = new ArrayCollection ();

              in a callback I receive data from the server and then i use this line of code to populate the datagrid.
              DGCollection.addItem (myObject);

              and then here is my datagrid xml:

              <mx:DataGrid dataProvider="{DGCollection}" id="MyDataGrid">
              <mx:DataGridColumn itemRenderer="MyRenderer1" dataField="label" headerText="headerText"/>

              <mx:DataGridColumn itemRenderer="MyRenderer2" dataField="label" headerText="headerText"/>

              The data loads just fine but I thought since the datagrid was bound to the collection that the ui what automatically update when you change anything on the collection.

              I've also tried this chunk of code to try to update the collection:
              DGCollection[myIndex].label = "test";

              which doesn't work

              is their a piece of code that I can use that updates the collection automatically?
              • 4. Re: Modify items in datagrid
                peterent Level 2
                The updates are not automatic. You must call itemUpdated to get the Collection to dispatch data change events. This is deliberate. See my response above.
                • 5. Re: Modify items in datagrid
                  Pigg_Man Level 1
                  var item:Object = DGCollection.getItemAt (myIndex);
                  item.statusLabel = "Changed Text"
                  DGCollection.setItemAt(item, CurrentJobIndex);

                  Yep... this is the way to do it... if you do a search in the help on
                  "Modifying data in DataGrid control"
                  you will get an example of how to do it... Thanks to everyone that helped