Kaoru
Kaoru

Reputation: 2883

Force checkbox to be unchecked or checked in datagridview

I want to force uncheck the checkbox in DataGridView if the entered amount is less than the known amount.

How do I do that?

Here is the code:

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");
                        }
                    }
                }
            }
        }

Here is the Amount Form code:

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

Here is the database looks like in DataGridView:

enter image description here

Here is the Amount Form looks like whenever I checked the CheckBox in DataGridView:

enter image description here

I want to when the Amount entered is not same as the known Amount, will uncheck the checkbox, and if the Amount entered is same as the known Amount, the checkbox will remain checked.

Right now, whenever the Amount entered is not same as the known Amount, the checkbox still remain checked.

Any help will be appreciated!

Thank you!

Upvotes: 0

Views: 596

Answers (1)

user3477392
user3477392

Reputation:

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;
           }

or when you define a new row, you can set to it a value like true or false

Upvotes: 1

Related Questions