Question 1: how to keep the scroll bar before the update after the datagridview is updated
A: Use the firstdisplayedscrollingrowindex and firstdisplayedscrollingcolumnindex attributes. See the following code.
Question 2: If the user continuously presses the shortcut key for updates at an instant, the user should not respond to every request. In the following code, lastupdatetime is used to solve the problem.
Private void dgv_keydown (Object sender, keyeventargs E)
{
If (E. keycode = keys. F5 & datetime. Now. Subtract (_ lastupdatetime). totalseconds> 1)
{
Int ROW = dgv. firstdisplayedscrollingrowindex;
Int Col = dgv. firstdisplayedscrollingcolumnindex;
Loadlog ();
If (dgv. Rows. Count> 0)
{
Row = row <0? 0: row;
If (row> = dgv. Rows. Count)
Row = dgv. Rows. Count-1;
Dgv. firstdisplayedscrollingrowindex = row;
Col = Col <0? 0: Col;
If (COL> dgv. Columns. Count)
Col = dgv. Columns. Count-1;
Dgv. firstdisplayedscrollingcolumnindex = Col;
}
}
}
Problem 3: display the row number:
You can draw in the rowpostpaint event of datagirdview.
Private void dgv_rowpostpaint (Object sender, datagridviewrowpostpainteventargs E)
{
Drawrowindex (sender, e );
}
Private void drawrowindex (Object sender, datagridviewrowpostpainteventargs E)
{
Rectangle rectangle = new rectangle (E. rowbounds. Location. X,
E. rowbounds. Location. y,
(Datagridview) sender). rowheaderswidth-4,
E. rowbounds. Height );
Textrenderer. drawtext (E. Graphics, (E. rowindex + 1). tostring (),
(Datagridview) sender). rowheadersdefaultcellstyle. Font,
Rectangle,
(Datagridview) sender). rowheadersdefaultcellstyle. forecolor,
Textformatflags. verticalcenter | textformatflags. Right );
}
Refer:
Automatic Adjustment of the datagridview column width and Row Height
Datagridview tip 1