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

Scrolling to previous selection

1 Answer 62 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Sean
Top achievements
Rank 1
Sean asked on 30 Jun 2009, 07:04 AM
Hi, I have a RadGridView that is bound to a DataTable.
I refresh the data binding every 30 seconds.
Currently before I refresh I store the currently selected rows and reselect them on rebinding to make the user experience as seamless as possible.
This works fine until I have enough items to cause it to scroll. When I have items selected at the bottom of the list, on refresh the selection disappears even though I have it added to the selectedrows collection, and the scrollbar is at the top with the item out of view. When I manually scroll down, I see that my previous selection has disappeared.
Also I tried using the code below as suggested in the forum to scroll to my selected row.
int visualRowsCount = this.radGridView1.GridElement.VisualRows.Count;   
  
this.radGridView1.GridElement.ScrollToRow(indexToScroll - visualRowsCount + 3);  
But my visualrows.count is always returned as zero. And when I call the ScrollToRow method, it always gives an outofbounds exception.
I am using the binding below where docs is a DataTable.
this.gridDocs.MasterGridViewTemplate.Columns.Clear(); 
this.gridDocs.MasterGridViewTemplate.DataSource = docs
Am I doing something wrong here?

1 Answer, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 03 Jul 2009, 09:15 AM
Hi Sean,

This is because the rows contained in the SelectedRows collection are valid only before changing the data source. You should save the row indexes instead. Please take a look at the code snippet below:

this.radGridView1.GridElement.BeginUpdate(); 
List<int> selectedRowsIndexes = new List<int>(); 
foreach (GridViewDataRowInfo row in this.radGridView1.SelectedRows) 
    selectedRowsIndexes.Add(this.radGridView1.Rows.IndexOf(row)); 
this.radGridView1.DataSource = GetDataSource(); 
foreach (int index in selectedRowsIndexes) 
    this.radGridView1.Rows[index].IsSelected = true
this.radGridView1.GridElement.EndUpdate(); 

I hope this helps. Should you have any other questions, I will be glad to help.

All the best,
Jack
the Telerik team

Instantly find answers to your questions on the newTelerik Support Portal.
Check out the tipsfor optimizing your support resource searches.
Tags
GridView
Asked by
Sean
Top achievements
Rank 1
Answers by
Jack
Telerik team
Share this question
or