Tip: The last scroll position (samwang) is maintained when the datagridview is rebound)

Source: Internet
Author: User

Problem:

Today, I encountered a problem in the project. I bound the able to the datagridview dview, one of which is the checkbox column. When I modify the value of this column, the cellvaluechanged event is triggered. When I process its datasource in this event, I find that although the value of the able corresponding to this cell has changed, its rowstate belongs to modified.

When I use datatable. Select () to query related data, the cell value still references old data. Although you try to set dataviewrowstate for the query, the result is also invalid (here there may be content that I don't know, it should be implemented as a matter of fact ).

The other method is to call able. acceptchanges () to receive data changes. At this time, you can get the correct query value.

However, this produces another problem, that is, the data is re-bound after the acceptchanges, causing the scroll bar position in the dview to move.

Then you need to change the position of the scroll bar.

 

Solution:

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.

Therefore, this value is recorded before acceptchanges, and then restored.

   int r = DataGridView.FirstDisplayedScrollingRowIndex;   DataTable.AcceptChanges();             this.pnl.dgvMain.FirstDisplayedScrollingRowIndex = r;

 

Author: samwang
Source: http://wangshenhe.cnblogs.com/
The copyright of this article is shared by the author and the blog Park. You are welcome to review it. During reprinting, you must provide a link to the original article clearly. Thank you for your cooperation.

Related Article

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.