6 Replies Latest reply on Jul 8, 2007 7:40 PM by Mitek17

    problem with Datagrid.selectedItem

    Mitek17
      Hi,

      Just bumped into the strange problem. Sometimes assignment to the datagrid.selectedItem property does not have any effect. The value becomes null and row is not selected in the grid. At the same time selectedIndex property works fine. And yes, I am sure that value is in the dataProvider.

      The piece of code which illustrates this problem:

      dg_List.selectedItem = m_Value;

      or

      for (var i:uint = 0; i < dg_List.dataProvider.length; i++)
      {
      if (CValueClass(dg_List.dataProvider.getItemAt(i)) == m_Value )
      {
      dg_List.selectedIndex = i;
      break;
      }
      }

      The documentation says that both properties are read-write.

      Does anybody know why does it happen?

      Regards,
      Mitek.
        • 1. Re: problem with Datagrid.selectedItem
          Mitek17 Level 1
          Looks like there is a bug in the Datagrid - you can have selectedItem != null and no selected rows on the screen in the datagrid.
          • 2. Re: problem with Datagrid.selectedItem
            ntsiii Level 3
            Only in rare situations can you set selectedItem.

            It will only work when you set it to a *reference* to an actual item in the dataProvider. You cannot set it to an object that simply has the same properties.

            To select an item using a property value of the dataProvider, yo must loop over the dataProvider and compare the value to the appropriate property, then use that indes to set selectedIndex.

            Tracy
            • 3. Re: problem with Datagrid.selectedItem
              Mitek17 Level 1
              Hi Tracy,

              The Value I am setting is an actual data from dataProvider - my example shows that comparison in the second case returns true.
              The bottom line is selectedItem = x does not work, and using selectedIndex = getSomehowIndex(x) works. In both cases x is the same object.

              Bug in the DataGrid?


              Cheers,
              Mitek.
              • 4. Re: problem with Datagrid.selectedItem
                ntsiii Level 3
                How are you setting m_Value?

                It does not matter if it is "actual data from dataProvider ", it must be a *reference* to a dataProvider item.

                The loop in the second case is the way you must do this.

                Now, this would work:

                dg_List.selectedItem = dg_List.dataProvider.getItemAt(2); //because it is settin it to a reference

                Tracy
                • 5. Re: problem with Datagrid.selectedItem
                  Mitek17 Level 1
                  Tracy,

                  If you look closely at the second option you can see that there is a comparison between getItemAt() and m_Value and it passes successfully. In the same time selectedItem = m_Value fails. All values here are refs to the same object.

                  OK, the task is - add new value to the dataprovider and make it selected in the grid. Easy.
                  I don't want to use second method (looping through values) because dataProvider is very large array and Flex perfomance is not the thing you should ignore.

                  Cheers,
                  Mitek.
                  • 6. Re: problem with Datagrid.selectedItem
                    Mitek17 Level 1
                    And yes, bugs database contains around 20 bugs in this area, but they all "hidden".