我创建了一个DataGridView控件,其中第一列是一个复选框。当我单击“在复选框中”时,代码将正确执行。但是,当单击发生在单元格中但不在复选框中时,代码将正确处理复选框的状态,但不会更新复选框本身,因此它仍处于单击之前的状态。
我该怎么纠正呢?
private void myDataGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1) return; //check if row index is not selected
DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
ch1 = (DataGridViewCheckBoxCell)myDataGrid.Rows[e.RowIndex].Cells[0];
if (ch1.Value == null)
ch1.Value = false;
switch (ch1.Value.ToString())
{
case "False":
ch1.Value = true;
break;
case "True":
ch1.Value = false;
break;
}
//BUT doesn't seem to matter what I do with ch1.Value. Inside the checkbox all is OK but
//outside, no.
}如果我单击该行上的另一个单元格,则将正确处理复选框。只有当我单击复选框单元格,而不是在复选框本身。
我的意思是(很抱歉),代码正确执行,但UI没有正确更新以反映更改。因此,如果复选框在未选中之前未选中,则在其后显示未选中。

病房也是,尽管它实际上是被检查的。
谢谢罗恩
发布于 2015-08-19 02:28:53
我百分之百肯定我做得很好。
/// <summary>
///データグリッドビューのチェックボックスのダブルクリックイベント
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Ichiran_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
this.Ichiran_CheckBoxCellClick(sender, e);
}
/// <summary>
/// データグリッドビューのチェックボックスのクリックイベント
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Ichiran_CellClick(object sender, DataGridViewCellEventArgs e)
{
this.Ichiran_CheckBoxCellClick(sender, e);
}
/// <summary>
/// セルの値自体がクリックされた際に発生するCellContentClickイベントの
/// コールバックメソッドです。
/// </summary>
/// <param name="sender">イベント送信元オブジェクト</param>
/// <param name="e"></param>
private void Ichiran_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
this.Ichiran_CheckBoxCellClick(sender, e);
}
/// <summary>
/// チェックボックスクリック
/// </summary>
private void Ichiran_CheckBoxCellClick(object sender, DataGridViewCellEventArgs e)
{
if (this.Ichiran.CurrentCell != null && this.Ichiran.CurrentCell is r_framework.CustomControl.DataGridCustomControl.DgvCustomCheckBoxCell)
{
if (e.RowIndex < 0)
{
return;
}
var cell = this.Ichiran["chb_delete", e.RowIndex];
if (cell.Value == null) cell.Value = false;
cell.Value = !(bool)cell.Value;
cell.ReadOnly = true;
cell.ReadOnly = false;
}
}
/// <summary>
/// CurrentCellDirtyStateChangedイベントハンドラでDataGridView.CommitEditメソッドを呼び出して値をコミットします。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Ichiran_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
this.Ichiran.CommitEdit(DataGridViewDataErrorContexts.Commit);
}https://stackoverflow.com/questions/20790640
复制相似问题