This is a migrated thread and some comments may be shown as answers.

Focusing item in RadGridView

2 Answers 225 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Peter
Top achievements
Rank 1
Peter asked on 31 Mar 2011, 12:42 AM
I am using a RadGridView, and want to set the selected item in the list programatically. I am doing this,

SelectedItem = blah;
ScrollIntoView();

And that works except that it doesn't move the focus to that item. There is still a small border around the original selected item. If I hit the up or down key on my keyboard it jumps to the item above or below the item I had selected originally.

I saw another forum post suggesting I do this,

ScrollIntoViewAsync(SelectedItem, r =>
                {
                    var row = r as GridViewRow;
 
                    if (row != null)
                    {
                        row.IsCurrent = true;
                        row.Focus();
                    }
                });


But that made no difference.

I have attached a screenshot showing the small remaining focus rectangle I mention. It is around TANUNDA DRIVE.

2 Answers, 1 is accepted

Sort by
0
Accepted
Dimitrina
Telerik team
answered on 31 Mar 2011, 01:21 PM
Hello Peter,

The problem occurs because when the CurrentItem is changed programmatic - the CurrentCell is not changed (and the arrow navigation uses the info from the CurrentCell). Therefore you need to set the current sell to some sell of the selected row:
  • this.dataGridView.CurrentCellInfo = new GridViewCellInfo(this.dataGridView.SelectedItem, this.dataGridView.Columns[0]);
    this.dataGridView.ScrollIntoView(this.dataGridView.SelectedItem);
    this.dataGridView.Focus();

Additionally regarding setting CurrentCellInfo you can take a look at: 
Best wishes,
Didie
the Telerik team
0
Peter
Top achievements
Rank 1
answered on 31 Mar 2011, 09:40 PM
That doesn't fix the problem 100%. This only works if I set the 'SelecteItem' to be an item that is already in view.

If I set the 'SelectedItem' to be an item that is not in view then it doesn't work.

Here is what I had originally after your advice above,

public class SearchableGridView : RadGridView
{
        private void SelectListItem(object currentListItem)
        {
            SelectedItem = currentListItem;
            CurrentCellInfo = new GridViewCellInfo(currentListItem, Columns[0]);
            ScrollIntoView(currentListItem);
            Focus();
        }
}
So it turns out that you have to set the 'CurrentCellInfo' to a cell that is IN VIEW or else the control sets to CurrentCellInfo to be the first record in the list.

So I tried this (scrolling it into view before setting CurrentCellInfo),

        private void SelectListItem(object currentListItem)
        {
            SelectedItem = currentListItem;
            ScrollIntoView(currentListItem);
            CurrentCellInfo = new GridViewCellInfo(SelectedItem, Columns[0]);
            Focus();
        }

That didn't work, but what did work was this,

        private void SelectListItem(object currentListItem)
        {
            SelectedItem = currentListItem;
            ScrollIntoView();
            Focus();
        }
 
        private void ScrollIntoView()
        {
            ScrollIntoViewAsync(SelectedItem, r =>
            {
                var row = r as GridViewRow;
 
                if (row != null)
                {
                    row.IsCurrent = true;
                    row.Focus();
                }
 
                CurrentCellInfo = new GridViewCellInfo(SelectedItem, Columns[0]);
            });
        }

So I guess it is a case of having to scroll the item into view, wait until that is done (which the second attempt above wasn't doing I guess), then set the CurrentCellInfo.
Tags
GridView
Asked by
Peter
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Peter
Top achievements
Rank 1
Share this question
or