【发布时间】:2017-05-23 16:12:09
【问题描述】:
我正在使用 c# 开发应用程序,我在 datagridview 中添加了两个复选框。我希望他们像两个单选按钮一样。如果选中第一个复选框,则自动取消选中另一个复选框 我尝试了一些代码,但对我不起作用。
private void datagridSB_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 15)
{
if (Convert.ToBoolean(datagridSB.CurrentRow.Cells[15].Value)==true)
{
datagridSB.CurrentRow.Cells[17].Value = false;
//uncheck the second checkbox
}
else if (Convert.ToBoolean(datagridSB.CurrentRow.Cells[15].Value)==false)
{
datagridSB.CurrentRow.Cells[17].Value = true;
//check the second checkbox
}
}
if (e.ColumnIndex == 17)
{
if (Convert.ToBoolean(datagridSB.CurrentRow.Cells[17].Value) == true)
{
datagridSB.CurrentRow.Cells[15].Value = false;
//uncheck the first checkbox
}
else if (Convert.ToBoolean(datagridSB.CurrentRow.Cells[17].Value) == false)
{
datagridSB.CurrentRow.Cells[15].Value = true;
//check the first checkbox
}
}
}
我得到错误 NullReferenceException was unhandled (object reference not set to an instance of an object) 请帮忙
【问题讨论】:
-
我假设这个网格是数据绑定的?如果是这样,请更改源,而不是网格。
-
不,不是。它只是一个空白的数据网格视图,其中包含一些用户应该输入数据的列,并且按钮保存点击数据应该立即保存
-
好的,当你放入断点时,你预期的变化会发生吗?如果没有,失败的原因是什么?
-
我知道代码应该在 CellValueChanged 中,但是当我在 CellValueChanged 中执行相同的代码时出现错误(对象引用未设置为对象的实例)
-
它没有重新调整条件。即使我取消选中它表示为真的复选框,它也总是会出现在条件 = true 的地方
标签: c# checkbox datagridview