Nabeel
Nabeel

Reputation: 81

how to check, checkbox is check or not in C# winform

 DataGridViewCheckBoxColumn GvCheckBox = new DataGridViewCheckBoxColumn();
        doWork.HeaderText = "Approved Cheque";
        doWork.FalseValue = "0";
        doWork.TrueValue = "1";
GvViewPendingCheque.Columns.Insert(0, doWork);
        GvViewPendingCheque.DataSource = objDataSet.Tables[0];

its working fine checkbox column has been added in grid with db data, i want to update data of all row where checkbox is check

MyTable Column: PaymentStatus MyTable Data: Pending

when admin check,checkbox PaymentStatus will update with Approved istead of Pending please help me, i have try this code but its not working:

  foreach (DataGridViewRow row in GvViewPendingCheque.Rows)
                    {

                        if ((bool)row.Cells[doWork.Name].Value == true)
                        {
                            MessageBox.Show("True");
                        }
                        else
                        {
                            MessageBox.Show("Failed...");
                        }
                    }

and

if ((bool)this.GvViewPendingCheque.Rows[0].Cells[0].Value == true)
            {
                MessageBox.Show("True");
            }
            else
            {
                MessageBox.Show("False");
            }

just want to know how to get checkbox 'check' rows

Upvotes: 1

Views: 1551

Answers (3)

tripex
tripex

Reputation: 11

If you cast your cell into a DataGridViewCheckBoxCell then you should be able to use the checked property to see if checked

cell1.checked == true

Upvotes: 0

Grant Winney
Grant Winney

Reputation: 66449

You're setting the false and true values to "0" and "1", respectively. But converting strings to boolean like you're attempting to do in the foreach loop will throw a FormatException.

Converting the numbers 0 and 1 to booleans, on the other hand, will return false and true.

DataGridViewCheckBoxColumn GvCheckBox = new DataGridViewCheckBoxColumn();

GvCheckBox.FalseValue = 0;  // replaced "0"
GvCheckBox.TrueValue = 1;   // replaced "1"

Upvotes: 1

Reuven Chacha
Reuven Chacha

Reputation: 889

you can use :

 if (Convert.ToBoolean(this.GvViewPendingCheque.Rows[0].Cells[0].Value) == true)
        ...

but i'm nut sure if this is a correct way to access a cell in your data grid view. easier way to access a cell [row, column]:

if (Convert.ToBoolean(this.GvViewPendingCheque[0,0].Value) == true)

Upvotes: 0

Related Questions