2 Replies Latest reply on Dec 4, 2006 12:05 PM by skrame

    datagrid vertical scrollbar problem

    skrame
      I wrote a search function for a datagrid component. The dataprovider is a ArrayCollection variable. If the item is found in the array, set the item to be selected in the datagrid. The problem is when the selected item is at bottom of the datagrid, the scrollbar doesn't refresh so I have to manually move the scrollbar to see the selected item. Here is part of my code:

      <mx:Script>
      <![CDATA[
      ......(populate accountData using dataservice) accountDS.fill(accountData);

      private function findAccountById(id:String):void
      {
      var size:int = accountData.length - 1;
      for(var i:int = 0; i< size; i++) {
      var account:Object = accountData.getItemAt(i);
      if(account.accountNumber.search(id) != -1)
      {
      dgAccount.selectedIndex = i;
      break;
      }
      }
      ]]>
      </mx:Script>
      <mx:ArrayCollection id="accountData"/>
      <mx:DataService id="accountDS" destination="accountlist"/>

      <mx:Panel >
      <mx:DataGrid id="dgAccounts" dataProvider="{accountData}" editable="false">
      <mx:columns>
      <mx:DataGridColumn dataField="accountNumber"/>
      <mx:DataGridColumn dataField="accountType" />
      </mx:columns>
      </mx:DataGrid>
      </mx:Panel>
        • 1. Re: datagrid vertical scrollbar problem
          jpwrunyan Level 1
          You need to look at the API for DataGrid. Particularly the property "verticalScrollPosition".

          The logic in your expectation that setting selectedIndex should update the scroll position is flawed. What if you selected an item with the mouse and then wanted to browse the other options? You would use the scroll bar for this. Your selection might disappear from the list but you would want it to stay selected. Thus the scroll position and the selected index are completely independent of one another.

          The point I am trying to make is this: you are required to set the verticalScrollPosition when you set selectedIndex if you want them to work in concert.
          • 2. datagrid vertical scrollbar problem
            skrame Level 1
            Thank you for your help! In my case, the datagrid has nothing but a long list of accounts. That's why i need to write a search function for it. I found the "verticalScrollPosition" property, it's inherited from the ScrollControlBase class. I added one line before break:

            dgAccounts.verticalScrollPosition = i ;

            And it works great now!

            Thanks!