none
How to Check the DataGridViewCheckBoxColumn if it's checked ?

    Question

  • I'm using one DataGridView in my windows form. I have one unbound column (of type DataGridViewCheckBoxColumn) and I want to programmatically know if the user has cheked this and then accordingly I want to process those rows.

    Any Help is appreciated.

     

    Wednesday, July 26, 2006 1:01 PM

Answers

  • You'll need to enumerate the rows of the grid and then convert the appropriate column to the correct type.

    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
       //Get the appropriate cell using index, name or whatever and cast to DataGridViewCheckBoxCell
       DataGridViewCheckBoxCell cell = row.Cells[colCheck] as DataGridViewCheckBoxCell;

       //Compare to the true value because Value isn't boolean
       if (cell.Value == cell.TrueValue)
          //The value is true
    }

    Michael Taylor - 7/26/06

    Wednesday, July 26, 2006 1:47 PM

All replies

  • You'll need to enumerate the rows of the grid and then convert the appropriate column to the correct type.

    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
       //Get the appropriate cell using index, name or whatever and cast to DataGridViewCheckBoxCell
       DataGridViewCheckBoxCell cell = row.Cells[colCheck] as DataGridViewCheckBoxCell;

       //Compare to the true value because Value isn't boolean
       if (cell.Value == cell.TrueValue)
          //The value is true
    }

    Michael Taylor - 7/26/06

    Wednesday, July 26, 2006 1:47 PM
  • Well. The following seems to have worked. I don't find anything cell.TrueValue  (TrueValue doesn't seem to be appearing) . By the way I'm using VB.Net. The cell.Value seems to be a Boolean value and if the checkbox is checked it returns True and if not it is nothing.

       If cell.Value == True Then
          //The value is true       
       End If


     

    Wednesday, July 26, 2006 5:49 PM
  • i am also looking for an answer to this point...
    Saturday, March 24, 2007 6:34 PM
  • try this one ,it will help u out

    u have to check the value of cell ,cell value will return the value as "1", or "0"

    this code is in C# ,chage as u want 

     

    foreach(DataGridViewRow row in grid.Rows)

    {

    if (row.Cells[1].Value!=null)

    {

    console.writeline(row.Cells[1].Value);

    }

    }

    Tuesday, April 03, 2007 12:43 PM
  •  

    This is a good one. You have to set the TrueValue first to true before you can check against it. Otherwise, it's null.

     

    foreach(DataGridViewRow row in grid.Rows)

    DataGridViewCheckBoxCell cbc = (DataGridViewCheckBoxCell ) row.Cells[0];

    cbc.TrueValue = true;

    if (cbc.Value == cbc.TrueValue)

    {

    ...

    }

    }
    Tuesday, October 21, 2008 12:34 AM
  • private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

    DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
    ch1 = (DataGridViewCheckBoxCell)dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0];

    if (ch1.Value == null)
    ch1.Value=false;
    switch (ch1.Value.ToString())
    {
    case "True":
    ch1.Value = false;
    break;
    case "False":
    ch1.Value = true;
    break;


    }
    MessageBox.Show(ch1.Value.ToString());
    }

    best solution to find if the checkbox in the datagridview is checked or not.. works perfectly
    want to tell thanks mail at nazeermjan@gmail.com
    Sunday, August 29, 2010 5:40 PM
  • If I need to loop through and check to see if a specific checkbox cell in any row is checked, this works for me.

    Keep your eye on the Cells index. Mine happened to be the 5th element.  i.e. Cells[4] 

    foreach(DataGridViewRow rows in dgvErrors.Rows)
    {
        if (Convert.ToBoolean(((DataGridViewCheckBoxCell)rows.Cells[4]).Value) == true)
        {
           //handle TRUE checkbox condition here
        }
    }

    I know, the convert and cast looks like a mess, but I've seen worse.  :-)

    • Proposed as answer by TigerJaw Thursday, July 14, 2011 3:12 PM
    Wednesday, May 04, 2011 7:16 PM
  • foreach(DataGridViewRow rows in Dgv_Alanlar.Rows)
    
    {
           if ((bool)rows.Cells["SEC"].FormattedValue)
               {
                    .... //true 
               }
    }
    • Proposed as answer by Alan Moseley Thursday, June 09, 2011 10:33 AM
    Thursday, June 02, 2011 1:04 PM
  • This method, Michael, only works as far as you are dealing with the clicking of that checkbox. Because it does not recognize whether we are checking the checkbox or unchecking it. So could you suggest a way out to the issue?

    Friday, February 21, 2014 7:06 AM