当用户选中datagridview中的复选框时,我想启用该按钮。我有一个复选框列。该按钮不在datagridview中。该按钮是默认的enable = false。
我的问题:当用户选中该复选框时,该按钮已启用,但当用户取消选中该复选框时,该按钮仍会保持启用状态。
我的代码:
void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
this.MainForm.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (this.MainForm.dataGridView1.Columns[e.ColumnIndex].Name == "Check" && e.RowIndex != -1)
{
this.MainForm.btnUpdate.Enabled = true;
}
else
{
this.MainForm.btnUpdate.Enabled = false;
}
}
void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
}
private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex > -1)
{
this.MainForm.dataGridView1.EndEdit();
}
}
答案 0 :(得分:0)
检查此处并根据您的组件进行修复。这是一个修补程序
void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (this.MainForm.dataGridView1.Columns[e.ColumnIndex].Name == "Check" && e.RowIndex != -1)
{
//here should be a code for enable/disable button, for ex
this.MainForm.btnUpdate.Enabled = !this.MainForm.btnUpdate.Enabled;
}
}
答案 1 :(得分:0)
我想你错过了if-checked声明...... <怎么样
if (this.MainForm.dataGridView1.Columns[e.ColumnIndex].Name == "Check" && e.RowIndex != -1 && this.MainForm.dataGridView1.Columns[e.ColumnIndex].Cells[e.RowIndex].value.ToString() == "True")
{
//enable button
}
else
{
//disable button
}