Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the position of my datagrid scrollbar in my winforms app?

In my C# winforms app, I have a datagrid. When the datagrid reloads, I want to set the scrollbar back to where the user had it set. How can I do this?

EDIT: I'm using the old winforms DataGrid control, not the newer DataGridView

like image 958
ScottG Avatar asked Dec 18 '08 16:12

ScottG


People also ask

How do I change the vertical scrollbar in winform?

No code is required. The scroll bars will only appear when there is a control outside the current view. Reading your description again, what you might be looking for is a large panel in your form. Add a Panel to your form and make set the location to 0,0 and the size to 992,1403.

How do I add a scroll in Winforms?

Just set the AutoScroll property of your Panel to true and it will handle adding the scrollbars for you. Bear in mind that your controls will need to be anchored to the Top of the Panel to cause Vertical scroll bars automatically, and the Left of the Panel to cause Horizontal scroll bars automatically.

How add DataGrid in Windows form?

Windows Forms DataGrid (SfDataGrid) control can be added to the application by dragging it from Toolbox and dropping it in Designer.


1 Answers

You don't actually interact directly with the scrollbar, rather you set the FirstDisplayedScrollingRowIndex. So before it reloads, capture that index, once it's reloaded, reset it to that index.

EDIT: Good point in the comment. If you're using a DataGridView then this will work. If you're using the old DataGrid then the easiest way to do that is to inherit from it. See here: Linkage

The DataGrid has a protected GridVScrolled method that can be used to scroll the grid to a specific row. To use it, derive a new grid from the DataGrid and add a ScrollToRow method.

C# code

public void ScrollToRow(int theRow) {     //     // Expose the protected GridVScrolled method allowing you     // to programmatically scroll the grid to a particular row.     //     if (DataSource != null)     {         GridVScrolled(this, new ScrollEventArgs(ScrollEventType.LargeIncrement, theRow));     } } 
like image 65
BFree Avatar answered Oct 17 '22 23:10

BFree