如何在 DataGridView C# 中取消选中所有复选框时清除文本框

How to clear textbox when all checkboxes are unchecked in DataGridView C#

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

    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的Cell Content Click中从上到下的代码。

SystemFormatException:

试试这个:

  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();