Click here to Skip to main content
13,295,051 members (85,567 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I want to search is the DataGridView checkbox checked or unchecked. If checked than I want to use that row. This is my problem.

My Code :
private void PracticalSubjectLoad()
              foreach (DataGridViewRow row in gdAddNewSubject.Rows)
                  DataGridViewCheckBoxCell CbxCell = (DataGridViewCheckBoxCell)row.Cells[1];
                  if(Convert.ToBoolean(CbxCell.Value) == true)
                  //if (Convert.ToBoolean((DataGridViewCheckBoxCell)row.Cells[1].Value) == true)
                  //    gdPractical.Rows.Add(row.Cells[1].Value.ToString());
              //for (int i = 0; i < gdAddNewSubject.Rows.Count; i++)
              //    //if (Convert.ToByte(db.dataSet.Tables["subject"].Rows[i][1].ToString()) == 1)
              //    //{
              //    //    ((DataGridViewCheckBoxCell)gdAddNewSubject.Rows[0].Cells[1]).Value = true;
              //    //}
              //    MessageBox.Show(gdAddNewSubject.Rows.Count.ToString());

          catch (Exception ex)

Error: Object cannot be cast from DBNull to other types.

Additional information this code is working only when my 1st-row is checked.
Please tell me how can I solve this problem
Posted 26-Jun-12 0:16am
sarthakm 10-Sep-15 23:04pm
suppose we check a checkbox in gridview ,it should enable delete button present outside.
son in which even shall we call it?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

DataGridViewCheckBoxCell CbxCell = row.Cells[1] as DataGridViewCheckBoxCell;
if (CbxCell!=null && !DBNull.Value.Equals(CbxCell.Value) && (bool)CbxCell.Value == true)
UL UL ALBAB 26-Jun-12 5:31am
Okay, its working but,
at the end sending me a message:
Object reference not set to an instance of an object.
Damith Weerasinghe 26-Jun-12 5:34am
in which line you get error?
UL UL ALBAB 26-Jun-12 6:10am
Not any line. This is working but, after complete task it's getting the error.
Damith Weerasinghe 26-Jun-12 6:14am
may be another issue on somewhere else, post it as another question if you can't find a solution.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

In this sample I assume that here it is a DataGridView with three columns.

FirstName | LastName | IsRegistered

The Codes below, adds the Registered Customer to a List<t>.

List<customer> list = new List<customer>();
foreach (DataGridViewRow row in this.GridView.Rows)
     // if a cell has never choosed so it is null 
     if ((row.Cells["IsRegistered"].Value) == null)
     if (((bool)row.Cells["IsRegistered"].Value == true))
         list.Add(new Customer() {       
            FirstName = (string)row.Cells["FirstName"].Value,
            LastName = (string)row.Cells["LastName"].Value

Let me know if you have any problem else.
Meysam Tolouee 10-Aug-12 16:15pm
For a person who vote:
What is down vote about, after 2 month?
If there is a problem with my code let me know.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171207.1 | Last Updated 26 Jun 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100