The checkbox column of the DataGridView in Winform. When the checkbox status is changed, the checkbox status value is obtained in real time.

Source: Internet
Author: User

I don't know if you have any experience like this. When you click or cancel the checkbox column of the datagridview dview, it is difficult to determine whether the status is selected or not, which makes it difficult to perform other operations, the solution is listed below:

The CurrentCellDirtyStateChanged and CellValueChanged events of the dview are mainly used.

The CurrentCellDirtyStateChanged event submits changes to the checkbox status.

The CellValueChanged event is used to perform other operations after the status is submitted, that is, after the cell value is changed.

(1). CurrentCellDirtyStateChanged Event code:

void PositionListDataView_CurrentCellDirtyStateChanged( object sender, EventArgs e)         {             DataGridView grid = sender as DataGridView;              if (grid != null )             {                 grid.CommitEdit(DataGridViewDataErrorContexts.Commit);             }         }

 

(2). CellValueChanged Event code:

Void PositionListDataView_CellValueChanged (object sender, DataGridViewCellEventArgs e) {DataGridView grid = sender as DataGridView; if (grid! = Null & e. rowIndex> = 0) {if (grid. columns [e. columnIndex]. name = "Check") {DataTable dt = grid. dataSource as DataTable; int pstnID = Convert. toInt32 (dt. rows [e. rowIndex] [1]); DataGridViewCheckBoxCell checkbox = grid. rows [e. rowIndex]. cells [e. columnIndex] as DataGridViewCheckBoxCell; // get the checkbox Column cell int result = 0; if (checkbox! = Null & checkbox. value. toString () = "1") {result = cuttingReport. updateR_RptRstnStandardAndBlendByCheck (this. reportID, pstnID, 1, 0);} else {result = cuttingReport. updateR_RptRstnStandardAndBlendByCheck (this. reportID, pstnID, 0, 0);} if (result <1) {MessageBox. show ("failed to modify", "prompt", MessageBoxButtons. OK, MessageBoxIcon. error );}}}}

In addition, the value of Name in grid. Columns [e. ColumnIndex]. Name = "Check" is added when the Columns of the DataGridView is generated:

New maid () {HeaderText = "Check", DataPropertyName = "Checked", Visible = true, Width = 45, Frozen = true, Name = "Check", TrueValue = 1, falseValue = 0, IndeterminateValue = 0}

Reference: http://www.cnblogs.com/gossip/archive/2008/12/02/1346047.html

 

 

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.