Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Checked Status DataGridViewCheckBox
 

Checked Status DataGridViewCheckBox

Is it possible to find out if a cell is checked or unchecked in the datagridviewcheckbox column and then use the results in a if statement?

I'm using c#

thanks,

Byran

Krossley  Monday, July 28, 2008 3:12 PM

Ok, I've installedC# Express. This works:

Code Snippet

if ((bool)this.dataGridView1.Rows[0].Cells[0].Value == true)

MessageBox.Show(this.dataGridView1.Rows[0].Cells[0].Value.ToString());//should print "true"

Lucian Baciu  Tuesday, July 29, 2008 12:54 PM

Hi!

If the first row, first columnis the one containing checkboxe:

Code Snippet

if (this.dataGridView1.Rows[0].Cells[0].Value == true)

//assume checked

Lucian Baciu  Monday, July 28, 2008 3:21 PM

Thanks for the reply but I get this error when i try to use the code.

Operator '==' cannot be applied to operands of type 'object' and 'bool'

any ideas?

Bryan

Krossley  Monday, July 28, 2008 10:20 PM

Do you check the right row?You have to specify the exact position where a checkbox is.

Lucian Baciu  Tuesday, July 29, 2008 7:48 AM

the check box is on every row and the second cell over from the left. I have Row[0] and Cell[1] I have also tride different combinations. The datagridview is databound to source does this matter?

Thanks,

Bryan

Krossley  Tuesday, July 29, 2008 11:27 AM

I'm sorry I do not have a Visual Studio installed here, but can you try this?:

Code Snippet

if (this.dataGridView1.Rows[0].Cells[0].Value.ToString()== "true")

MessageBox.Show(this.dataGridView1.Rows[0].Cells[0].Value.ToString());//should print "true"

Do you still get the error?
Lucian Baciu  Tuesday, July 29, 2008 11:54 AM

Here is what I get now.

error

Object reference not set to an instance of an object.

Krossley  Tuesday, July 29, 2008 12:01 PM

Then you're refering to a cell that it does not exist. Check to be in an existing cell.

Lucian Baciu  Tuesday, July 29, 2008 12:03 PM

I think I need to see if the cell is null before calling. How do I do that.

Bryan

Krossley  Tuesday, July 29, 2008 12:24 PM

Ok, I've installedC# Express. This works:

Code Snippet

if ((bool)this.dataGridView1.Rows[0].Cells[0].Value == true)

MessageBox.Show(this.dataGridView1.Rows[0].Cells[0].Value.ToString());//should print "true"

Lucian Baciu  Tuesday, July 29, 2008 12:54 PM

Ok that worked. I am still have some problem getting everything to work. I have a dataGridView with a checkbox when the user checks the checkbox I have the cellclick event fire off to the database and change the column to true which works as expected but the the user clicks the checkbox again to remove the check the cellclick event does not update to false. I am posting my code.

Thanks,

Bryan

Code Snippet

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

{

DataGridView dgv1 = new DataGridView();

string idstring = dataGridView1.SelectedRows[0].Cells["COIN_ID"].Value.ToString();

int parseid = Int32.Parse(idstring);

int id = Convert.ToInt32(parseid);

var dataContext = new LINQTESTDataContext();

COPPER check = dataContext.COPPERs.SingleOrDefault(c => c.COIN_ID == id);

if ((bool)this.dataGridView1.Rows[0].Cells[1].Value == false)

check.CheckBox = true;

else if ((bool)this.dataGridView1.Rows[0].Cells[1].Value == true)

check.CheckBox = false;

textBox1.Text = idstring;

dataContext.SubmitChanges();

}

Krossley  Wednesday, July 30, 2008 1:39 PM

Please post this in another thread. One question per thread please.

Regards,

Lucian Baciu  Wednesday, July 30, 2008 1:48 PM
hi, i am also facing the similar problem. My code is here below :


for (int i = 0; i < dataGriedView1.Rows.Count; i++)
{
if ((bool)this.dataGriedView1.Rows[i].Cells[0].Value == false)

MessageBox.Show(this.dataGriedView1.Rows[i].Cells[0].Value.ToString());
}


I'm getting error :

Nullreferenceexception was unhandeled.
Object reference not set to an instance of an object.

Please help.
GauravKapoor  Sunday, March 08, 2009 9:00 AM

You can use google to search for other answers

Custom Search

More Threads

• How to update data (checkbox on Datagrid) into Database ?
• DataSet's Position changes but TextBox value is unchanged
• DataGridView Filter
• DB app design suggestion
• Datasets - Dynamic Database binding
• Filling a typed dataset
• How do i populate a datagridview cell with an unbound data after input information on a cell?
• Problem with dynamic column in DatagridView
• What Mapping Name do I use when DataGrid.DataSource is IList<InterfaceItem>?
• Problem when saving DataSet to XML by using its WriteXml() method, e.g. inconsistent xml structure and returned error.