Click here to Skip to main content
15,922,533 members
Please Sign up or sign in to vote.
5.00/5 (1 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 :
C#
private void PracticalSubjectLoad()
      {
          try
          {
              foreach (DataGridViewRow row in gdAddNewSubject.Rows)
              {
                  DataGridViewCheckBoxCell CbxCell = (DataGridViewCheckBoxCell)row.Cells[1];
                  if(Convert.ToBoolean(CbxCell.Value) == true)
                      gdPractical.Rows.Add(row.Cells[1].Value.ToString());
                  //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)
          {
              MessageBox.Show(ex.Message);
          }
      }


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
Comments
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?

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>.


C#
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)
         continue;

     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.
 
Share this answer
 
v2
Comments
[no name] 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.
C#
DataGridViewCheckBoxCell CbxCell = row.Cells[1] as DataGridViewCheckBoxCell;
if (CbxCell!=null && !DBNull.Value.Equals(CbxCell.Value) && (bool)CbxCell.Value == true)
{
    gdPractical.Rows.Add(CbxCell.Value.ToString());
}
 
Share this answer
 
Comments
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.
DamithSL 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.
DamithSL 26-Jun-12 6:14am    
may be another issue on somewhere else, post it as another question if you can't find a solution.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900