sty/100
[C#] Easy solution for AutoScroll in GridView
Poking around standard WinForms GridView control I found it have not any „Autoscroll” properties. A proper approach would be create new control inherited from base GridView with altered component drawing and focusing etc, but I didn’t have time and will to do this, especially for my extremally simple FileSorter tool. So I came up with easy solution.
The Problem:
Application have a grid, which is in real time adding new rows. When it’s view area height is reached, more rows are showed hidden „below” the view area of grid, and user is forced to use vertical scroll bar to scroll to them.
Solution:
We can use „FirstDisplayCell” property of DataGridView property
Code:
if(_grid.Rows.Count>0 && _grid.Rows[_grid.Rows.Count-1].Cells.Count>0) _grid.FirstDisplayedCell = _grid.Rows[_grid.Rows.Count-1].Cells[0];
Solution in 2 lines of code
Additional Notes:
Method is thread safe and updates GUI properly using Invoke while invoking on a _grid object