C# 数据网格视图两个复选框列的作用类似于两个单选按钮



我正在使用 C# 开发应用程序 我在数据网格视图中添加了两个复选框。 我希望它们像两个单选按钮。 如果选中第一个复选框,则自动取消选中另一个复选框 我尝试了一些代码,但对我不起作用。

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 
}
}   
}

我收到错误空引用异常未处理(对象引用未设置为对象的实例) 请提供任何帮助

我不敢相信我错过了,秒...您正在转换为布尔整个条件:

Convert.ToBoolean(datagridSB.CurrentRow.Cells[17].Value = true)  

将结束参数移动到值之后,并且在 C# 中将 == 而不是 "=",因此:

Convert.ToBoolean(datagridSB.CurrentRow.Cells[17].Value) == true

您的代码正在设置值,并评估您的布尔值...

我仍然不明白为什么你想使用两个复选框,而一个复选框似乎只需要。您可能会遇到的问题是双重的。首先,使用DataGridViewCurrentRow属性是有风险的,它可能会返回意想不到的结果,我觉得这可能是问题之一。另一个问题是使用CellValueChanged事件来捕获用户更改其中一个复选框的时间。在某些情况下,选中该复选框时,此事件似乎返回 false。由于这种奇怪的行为,我猜CellValueChanged事件对于你想做的事情来说并不是最好的。

若要解决这两个问题,建议使用从事件返回的RowIndex,而不是CurrentRow属性。此外,我们要更改为的事件是CellContentClick事件。当用户单击单元格的内容时,将触发此事件。对于复选框单元格,这会将其值从选中更改为未选中,反之亦然。在这里,我们只需要检查复选框是否已选中。如果选中,则将另一个复选框设置为未选中,反之亦然。

下面的代码演示了上面描述的内容。有一个包含三列的DataGridView,最后两列是复选框。CellContentClick事件被连接起来以捕获何时单击单元格的内容,然后我们只需检查单击的单元格是否在第 1 列或第 2 列中,然后适当地设置另一个复选框。我添加了CellContentDoubleClick事件来调用CellContentClick事件,否则用户将能够快速双击复选框,而不为设置另一个复选框留出时间,从而导致两个复选框具有相同的值。

private void datagridSB_CellContentClick(object sender, DataGridViewCellEventArgs e) {
int curRow = e.RowIndex;
if (e.ColumnIndex == 1) {
DataGridViewCheckBoxCell cbc = datagridSB.Rows[curRow].Cells[1] as DataGridViewCheckBoxCell;
bool isChecked = (bool)cbc.EditedFormattedValue;
if (isChecked) {
datagridSB.Rows[curRow].Cells[2].Value = false;
} else {
datagridSB.Rows[curRow].Cells[2].Value = true;
}
}
if (e.ColumnIndex == 2) {
DataGridViewCheckBoxCell cbc = datagridSB.Rows[curRow].Cells[2] as DataGridViewCheckBoxCell;
bool isChecked = (bool)cbc.EditedFormattedValue;
if (isChecked) {
datagridSB.Rows[curRow].Cells[1].Value = false;
} else {
datagridSB.Rows[curRow].Cells[1].Value = true;
}
}
}
private void datagridSB_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e) {
datagridSB_CellContentClick(sender, e);
}

希望这有帮助。

private void datagridSB_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (datagridSB.Columns[e.ColumnIndex].Name == "withCashSB")
{
DataGridViewCheckBoxCell buttonCell = (DataGridViewCheckBoxCell)datagridSB.Rows[e.RowIndex].Cells["withProCSB"];
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)datagridSB.Rows[e.RowIndex].Cells["withCashSB"];
buttonCell.Value = !(Boolean)checkCell.Value;
datagridSB.Invalidate();
}
else if (datagridSB.Columns[e.ColumnIndex].Name == "withProCSB")
{
DataGridViewCheckBoxCell buttonCell = (DataGridViewCheckBoxCell)datagridSB.Rows[e.RowIndex].Cells["withCashSB"];
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)datagridSB.Rows[e.RowIndex].Cells["withProCSB"];
buttonCell.Value = !(Boolean)checkCell.Value;
datagridSB.Invalidate();
}
}
private void datagridSB_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (datagridSB.IsCurrentCellDirty)
{
datagridSB.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}

这是使两个复选框像两个单选按钮一样的答案..

最新更新