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