Categories
C# Uncategorized

Scrolling a datagrid to a specific row (or to the bottom)


Warning: Illegal string offset 'language' in /app/wp-content/plugins/igsyntax-hiliter/classes/frontend.php on line 510

Warning: ksort() expects parameter 1 to be array, string given in /app/wp-content/plugins/igsyntax-hiliter/classes/frontend.php on line 513

Warning: Illegal string offset 'language' in /app/wp-content/plugins/igsyntax-hiliter/classes/frontend.php on line 510

Warning: ksort() expects parameter 1 to be array, string given in /app/wp-content/plugins/igsyntax-hiliter/classes/frontend.php on line 513

Warning: Illegal string offset 'language' in /app/wp-content/plugins/igsyntax-hiliter/classes/frontend.php on line 510

Warning: ksort() expects parameter 1 to be array, string given in /app/wp-content/plugins/igsyntax-hiliter/classes/frontend.php on line 513

There are 2 ways you can do this. In the first method, you can derive a class from datagrid and use the protected method GetVScroll(). In the second method, you can just do this :

  1. this.dataGridView1.CurrentCell = this.dataGridView1[0, rowIndex];

If you want to scroll to the bottom then do this :

  1. this.dataGridView1.CurrentCell = this.dataGridView1[0, this.dataGridView.Rows.count - 1];

I also found a piece of code which had a few lines (to select a row, not just scroll):

  1. dataGridView1.FirstDisplayedScrollingRowIndex = index;
  2. dataGridView1.Refresh();
  3. dataGrid.CurrentCell = dataGrid.Rows[index].Cells[0];
  4. dataGrid.Rows[index].Selected = true;

By Moazzam

Moazzam is a software developer working in the big apple. He has over a decade of experience in development. While most of his work involves developing web applications, he does development for Android and Windows Mobile in his spare time.

2 replies on “Scrolling a datagrid to a specific row (or to the bottom)”

I think I found the best way to scroll to the bottom:

In the `DataContextChanged` event put this in:

myDataGrid.ScrollIntoView(CollectionView.NewItemPlaceholder);

Easy huh?

This is why it works: On every data grid there is a place at the bottom of the DataGrid where you can add a new item to your list that it’s bound to. That is a CollectionView.NewItemPlaceholder, and there will only be one of those in your DataGrid. So you can just scroll to that.

Leave a Reply

Your email address will not be published. Required fields are marked *