【问题标题】:C# datagridview two checkboxes columns act like two radio buttonsC# datagridview 两个复选框列就像两个单选按钮
【发布时间】: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


【解决方案1】:

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

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

将结束括号移动到值之后,并且 == 而不是 C# 中的“=”,所以:

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

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

【讨论】:

  • 它没有用,但现在它去了条件 = false 的地方。我在 CellValueChanged 事件上尝试了相同的代码我仍然得到错误(对象引用未设置为对象的实例)
  • 好的,那么什么部分是空的?我的猜测是 CurrentRow。
  • 是的,它是 currentRow。线
  • 很高兴,如果您能将此标记为已回答,我们将不胜感激。 :-)
【解决方案2】:

我仍然不明白为什么要使用两个复选框,而一个似乎就足够了。您可能遇到的问题是双重的。首先,使用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);
}

希望这会有所帮助。

【讨论】:

  • 谢谢,这很有帮助。它帮助我解决了这个问题:)
【解决方案3】:
    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);
        }
    }

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

【讨论】:

    猜你喜欢
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 1970-01-01
    • 2016-04-17
    • 2019-01-21
    • 1970-01-01
    相关资源
    最近更新 更多