4 Replies Latest reply on Jun 16, 2006 8:25 AM by Timothy Huertas

    DataGrid not refreshing

    Timothy Huertas
      I have a datagrid whose dataProvider is an instance of ArrayCollection. When I change data in the selectedItem the ArrayCollection bound to the datagrid is updated, however Flex does not update(repaint) this new value in the datagrid’s cell.

      In the example below I have a list of people bound to my data grid. When I change the selected person’s name the bound object is updated, but the data in the datagrid is not updated.

      I came up with a hack to fix this. If I manually reset the dataprovider on the grid the grid is updated. I am pretty sure this is a bug. Any feedback is welcome. Below is the test case I wrote up.


      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical" activate="testActivate()">
      <mx:Script>
      <![CDATA[
      import mx.collections.ArrayCollection;

      [Bindable]
      public var people:ArrayCollection;


      private function testActivate() : void
      {
      people = new ArrayCollection();
      var one:Object = new Object();
      one.name = "Bob";
      one.age = "12";

      var two:Object = new Object();
      two.name = "Bob";
      two.age = "12";

      var three:Object = new Object();
      three.name = "Bob";
      three.age = "12";

      people.addItem(one);
      people.addItem(two);
      people.addItem(three);
      }

      private function changeNameClick() : void
      {
      testDataGrid.selectedItem.name = txtName.text;
      //uncomment to see hack in action
      //this.testDataGrid.dataProvider = people;
      }

      ]]>
      </mx:Script>
      <mx:DataGrid id="testDataGrid" dataProvider="{people}">
      <mx:columns>
      <mx:DataGridColumn headerText="Name" dataField="name"/>
      <mx:DataGridColumn headerText="Age" dataField="age"/>
      </mx:columns>
      </mx:DataGrid>

      <mx:Label text="Selected Person's Name-- {testDataGrid.selectedItem.name}" />
      <mx:Label text="Enter Name" /> <mx:TextInput id="txtName"/>
      <mx:Button enabled="{testDataGrid.selectedItem}" click="changeNameClick()" label="change name"/>

      </mx:Application>


      Thanks
      Timothy Huertas
        • 1. Re: DataGrid not refreshing
          Timothy Huertas Level 1
          I was able to get around this by putting testDataGrid.executeBindings(true); after I changed the data in the data source.
          • 2. Re: DataGrid not refreshing
            Level 7
            Hi Timothy,
            > I have a datagrid whose dataProvider is an instance of ArrayCollection. When I
            > change data in the selectedItem the ArrayCollection bound to the datagrid is
            > updated, however Flex does not update(repaint) this new value in the datagrid?s
            > cell.

            When you change a property of an item in an ArrayCollection (or any
            other collection type), I believe you need to call the itemUpdated(item)
            method on the ArrayCollection. There's also a way to do this by
            implementing a notification interface on the collection's contents, but
            calling itemUpdated() is the simplest way to do it.

            Hope this helps.

            --
            nj
            Flex Builder team
            • 3. Re: DataGrid not refreshing
              Timothy Huertas Level 1
              NJ

              Thanks for the reply. You are correct calling itemUpdated does fix the problem in the above situation.

              Unfortunately what I posted was a "scaled down" version of what I thought my problem was. I now realize that to correctly recreate the problem the ArrayCollection has to be an aggregate of a Bindable Object. I modified the example to hold on to a bindable instance of an Object "house" that has an ArrayCollection of Objects "people". Notice that when the example below is ran the datagrid remains empty even after I enoke itemUpdated on the ArrayCollection.

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical" activate="testActivate()">
              <mx:Script>
              <![CDATA[
              import mx.collections.ArrayCollection;

              [Bindable]
              public var house:Object;



              private function testActivate() : void
              {
              house = new Object();
              house.people = new ArrayCollection();
              var one:Object = new Object();
              one.name = "Bob";
              one.age = "12";

              var two:Object = new Object();
              two.name = "Bob";
              two.age = "12";

              var three:Object = new Object();
              three.name = "Bob";
              three.age = "12";

              house.people.addItem(one);
              house.people.addItem(two);
              house.people.addItem(three);

              house.people.itemUpdated(one);
              house.people.itemUpdated(two);
              house.people.itemUpdated(three);

              }

              private function changeNameClick() : void
              {
              testDataGrid.selectedItem.name = txtName.text;
              }

              ]]>
              </mx:Script>
              <mx:DataGrid id="testDataGrid" dataProvider="{house.people}">
              <mx:columns>
              <mx:DataGridColumn headerText="Name" dataField="name"/>
              <mx:DataGridColumn headerText="Age" dataField="age"/>
              </mx:columns>
              </mx:DataGrid>

              <mx:Label text="Selected Person's Name-- {testDataGrid.selectedItem.name}" />
              <mx:Label text="Enter Name" /> <mx:TextInput id="txtName"/>
              <mx:Button enabled="{testDataGrid.selectedItem}" click="changeNameClick()" label="change name"/>

              </mx:Application>

              Thanks

              Timothy Huertas
              • 4. Re: DataGrid not refreshing
                Timothy Huertas Level 1
                I figured it out. The problem I encountered was being caused by something else. I was overthinking things. Thanks for the help.