Several questions about the datagridview

Source: Internet
Author: User

 

 

 

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

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.