【问题标题】:Force checkbox to be unchecked or checked in datagridview在datagridview中强制取消选中或选中复选框
【发布时间】:2014-04-18 18:32:38
【问题描述】:

如果输入的金额小于已知金额,我想强制取消选中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中的数据库:

这是Amount 表单,每当我在DataGridView 中检查CheckBox 时的样子:

我想当输入的Amount与已知的Amount不同时,将取消选中该复选框,如果输入的Amount与已知的Amount相同,则该复选框将保持选中状态。

目前,只要输入的Amount 与已知的Amount 不同,复选框仍处于选中状态。

任何帮助将不胜感激!

谢谢!

【问题讨论】:

    标签: c# winforms checkbox datagridview


    【解决方案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

    【讨论】:

    • 已解决,先生。谢谢
    猜你喜欢
    • 2017-01-24
    • 1970-01-01
    • 2016-03-21
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    • 2019-09-25
    • 1970-01-01
    • 2012-10-27
    相关资源
    最近更新 更多