【问题标题】:How to clear textbox when all checkboxes are unchecked in DataGridView C#在DataGridView C#中未选中所有复选框时如何清除文本框
【发布时间】:2015-02-28 21:33:30
【问题描述】:

当所有复选框都未选中时,我应该如何清除文本框。

    private void qualitySetupDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

        DataGridView data = this.qualitySetupDataGridView;
        int CurrentRow = data.CurrentRow.Index;

        foreach (DataGridViewRow Row in data.Rows) {
            object cell = Row.Cells[5].EditedFormattedValue;

            if (Convert.ToBoolean(cell) == false) {
                textBox1.Clear();


            }

添加了更多代码。为了更容易理解,我添加了更多代码。

if (Convert.ToBoolean(data.Rows[CurrentRow].Cells[5].EditedFormattedValue) == true)
 {

EndsPerInch = double.Parse(data.Rows[CurrentRow].Cells[4].Value.ToString());
                Sum = Sum + EndsPerInch;
                count++;
                Average = Sum / count;
                FinalValue = Math.Round(Average, 2);
                textBox1.Text = Convert.ToString(FinalValue);

            }

else if (Convert.ToBoolean(data.Rows[CurrentRow].Cells[5].EditedFormattedValue) == false)
            {
                EndsPerInch = double.Parse(data.Rows[CurrentRow].Cells[4].Value.ToString());
                Sum = Sum - EndsPerInch;
                count--;
                Average = Sum / count;
                FinalValue = Math.Round(Average, 2);
                textBox1.Text = Convert.ToString(FinalValue);
         //       textBox1.Clear();

                count = 0;
                Sum = 0;
                EndsPerInch = 0;
                Average = 0;
                FinalValue = 0;


            }

这是DataGridView的单元格内容点击中从上到下的代码。

SystemFormatException:

【问题讨论】:

  • 我们需要更多关于如何设置数据绑定的信息。请向我们展示代码。
  • 添加了代码。现在检查。

标签: c# checkbox datagridview


【解决方案1】:

试试这个:

  bool shouldClearTextBox = true;
  foreach (DataGridViewRow Row in data.Rows)
  {
    object cell = Row.Cells[5].EditedFormattedValue;
    // If the particular checkbox isn't cleared, shouldClearTextBox is set to false
    shouldClearTextBox &= !Convert.ToBoolean(cell);
  }
  if (shouldClearTextBox)
    textBox1.Clear();

【讨论】:

  • &= 是一个布尔表达式。它要求两个参数都为真,返回真,否则返回假。其实就是&就是布尔运算符,&=等同于res = var1 & var2;
  • 我收到系统格式异常。另外,我在原始帖子中添加了更多代码。
  • 好的,我只是贡献你现有的代码,而不运行它。你能发布整个异常吗?
  • 添加到我原来的帖子中。
  • 好的,没有你的完整代码就不容易理解了。您是否验证 Convert.ToBoolean(cell) 有效?
猜你喜欢
  • 1970-01-01
  • 2020-11-22
  • 2014-11-06
  • 1970-01-01
  • 2014-01-10
  • 2016-01-16
  • 1970-01-01
  • 2014-11-06
  • 1970-01-01
相关资源
最近更新 更多