C #: Keep the last scroll position when the datagridview is rebound

Source: Internet
Author: User
Although the datagridview datagrihas the verticalscrollbar attribute, It is a protected object and cannot be accessed externally. After reading the attributes of the datagridview, it is found that firstdisplayedscrollingrowindex is the value of the scroll bar, the Row Height of the datagridview multiplied by firstdisplayedscrollingrowindex, which indicates the height of the customer zone.

The following is an example of a vertical scroll bar:

Int _ scrollvalue = 0;

Private void dgvvehicles_scroll (Object sender, scrolleventargs E)
{
If (E. scrollorientation = scrollorientation. verticalscroll)
{
_ Scrollvalue = E. newvalue;
}
}

Private void binddata ()
{
// Set the data source
...
If (dgvvehicles. Rows. Count> _ scrollvalue)
Dgvvehicles. firstdisplayedscrollingrowindex = _ scrollvalue;
}

Another way is to set currentcell. setting this value will cause the dview to jump to the row where the cell is located. The method is to traverse each row to find the unique cell and set it to the current cell.

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.