强制复选框在数据网格视图中取消选中或选中

时间:2014-04-18 18:32:39

标签: c# winforms checkbox datagridview

如果输入的金额低于已知金额,我想强制取消选中DataGridView中的复选框。

我该怎么做?

以下是代码:

private void Amount(object sender, DataGridViewCellEventArgs e)
        {
            _columnIndexes = new List<int>(new int[] { 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 } );

            foreach (int index in _columnIndexes)
            {
                if (e.ColumnIndex == index)
                {
                    var value = ((DataGridView)sender).Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();

                    if (value != null)
                    {
                        if (Convert.ToBoolean(value) == true)
                        {
                            _amount.ShowDialog();

                            if (_amount._price != _amount._amountPrice)
                            {
                                MessageBox.Show("You have entered an amount: Rp " + _amount._price + "\nColumn Number: " + e.ColumnIndex + "\nYou have provided the wrong amount!", "Cannot proceed");

                                //Uncheck the checkbox
                            }

                            else
                            {
                                MessageBox.Show("You have entered an amount: Rp " + _amount._price + "\nColumn Number: " + e.ColumnIndex + "\nYou have provided the correct amount!", "Proceeded");

                                //Check the checkbox
                            }
                        }

                        else
                        {
                            MessageBox.Show("Not Checked. \n" + "Column Number: " + e.ColumnIndex + "", "Not Checked");
                        }
                    }
                }
            }
        }

以下是Amount表单代码:

    public int _price;

    public int _amountPrice = 500000;

    public Amount()
    {
        InitializeComponent();
    }

    private void Amount_Load(object sender, EventArgs e)
    {
        this.numericTextBox1.Text = "0";
    }

    private void button1_Click(object sender, EventArgs e)
    {
        _price = Convert.ToInt32(this.numericTextBox1.Text);

        if (this.numericTextBox1.Text == "0")
        {
            MessageBox.Show("You cannot leave the amount blank or as zero (0)", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            this.numericTextBox1.Focus();
        }

        else
        {
            this.Hide();

            this.Close();
        }
    } 

以下是DataGridView中的数据库:

enter image description here

每当我检查Amount中的CheckBox时,DataGridView表单就会出现:

enter image description here

我希望当输入的Amount与已知的Amount不同时,取消选中该复选框,以及输入的Amount是否与已知的Amount相同,复选框将保持选中状态。

目前,只要输入的Amount与已知的Amount不同,该复选框仍会保持选中状态。

任何帮助将不胜感激!

谢谢!

1 个答案:

答案 0 :(得分:1)

string yourValue="x";
           if (dataGridView1.Rows[0].Cells[1].Value.ToString() == yourValue)
           {
            dataGridView1.Rows[0].Cells[1].Value = true;

            //or
            //dataGridView1.Rows[0].Cells[3].Value = false;
           }

或者当您定义新行时,可以将其设置为true或false

之类的值