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

ScrollToRow Index Out-of-Range Error

2 Answers 201 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jeremy Murtishaw
Top achievements
Rank 1
Jeremy Murtishaw asked on 06 Jul 2009, 10:33 PM
Hello,

I'm getting an index out of range error on a scroll-to command that doesn't seem to make sense. In the block of code just before the ScrollToRow command, I am setting the datasource equal to null, and then re-initializing the datasource. Next, I am checking if the number of rows exceeds the number that can be displayed, and if so, I wish to scroll to the bottom to display only the last rows. Here is the code:

                        grdRecords.DataSource = objRecordList; 
                        AlignRecordGrid(); 
                        int iVisibleRows = grdRecords.GridElement.RowsPerPage; 
                         
                        if (grdRecords.Rows.Count > iVisibleRows) 
                        { 
                            grdRecords.GridElement.ScrollToRow(objRecordList.Count - iVisibleRows + 3); 
                            //grdRecords.FirstDisplayedScrollingRowIndex = objRecordList.Count - iVisibleRows + 3; 
                        } 

I run into an error here every time saying that the index is out of range. This is strange to me since in the example, the number of rows is 21 and the rows per page is 14. When the gridview is a windows gridview, the commented out line did the trick just fine.

Any help would be appreciated!

Thanks!
Jeremy

2 Answers, 1 is accepted

Sort by
0
Accepted
Jack
Telerik team
answered on 07 Jul 2009, 12:34 PM
Hi Jeremy,

RadGridView creates its rows asynchronous. Because of this, they aren't created immediately after setting the DataSource property. You should call the Application.DoEvents method to force the creation of rows. Here is the modified version of your code:

grdRecords.DataSource = objRecordList; 
AlignRecordGrid();  
Application.DoEvents(); 
int iVisibleRows = grdRecords.GridElement.RowsPerPage; 
if (grdRecords.Rows.Count > iVisibleRows) 
    grdRecords.GridElement.ScrollToRow(objRecordList.Count - iVisibleRows + 3); 
    //grdRecords.FirstDisplayedScrollingRowIndex = objRecordList.Count - iVisibleRows + 3;  
}  

I hope this helps. Should you have any other questions, please write us back.

Kind regards,
Jack
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Jeremy Murtishaw
Top achievements
Rank 1
answered on 08 Jul 2009, 08:51 PM
That worked great, thanks!
Jeremy
Tags
GridView
Asked by
Jeremy Murtishaw
Top achievements
Rank 1
Answers by
Jack
Telerik team
Jeremy Murtishaw
Top achievements
Rank 1
Share this question
or