运行 Datagrid 复选框为 checked/unchecked 时的代码,并获取更改后的值

Run code when a Datagrid checkbox is checked/unchecked, and get the changed value

当数据网格上的复选框为 check/unchecked 时,我需要执行代码,没有太多要说的了。

所以我有:

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
        txtCCs.Text = String.Empty;
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (Convert.ToBoolean(row.Cells[CC.Name].Value) == true)
            {
                txtCCs.Text += row.Cells[3].Value.ToString().Trim() + ", ";
            }
        }
    }

它在一个新的裸项目中工作,但在我的实际项目中,它 运行s 但没有看到更改的值。因此,如果我点击一个框,没有任何反应,点击另一个它现在可以看到第一个被选中,但错过了被点击以触发它的那个。即使我的代码中有 CommitEdit。

编辑:我完全删除了 CellContentClick 代码并删除了该功能,现在可以使用了。不确定是什么问题,因为它仍然 运行,只是不执行 commitEdit。

您需要先提交编辑:

void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) {
  dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
  txtCCs.Text = String.Empty;
  foreach (DataGridViewRow row in dataGridView1.Rows) {
    if (Convert.ToBoolean(row.Cells[CC.Name].Value) == true) {
      txtCCs.Text += row.Cells[3].Value.ToString().Trim() + ", ";
    }
  }
}