Click here to Skip to main content
11,583,859 members (47,830 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 C#3.0 C#
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()
        {
            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 25-Jun-12 23:16pm
Rate this: bad
good
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)
{
    gdPractical.Rows.Add(CbxCell.Value.ToString());
}
  Permalink  
Comments
UL UL ALBAB at 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 at 26-Jun-12 5:34am
   
in which line you get error?
UL UL ALBAB at 26-Jun-12 6:10am
   
Not any line. This is working but, after complete task it's getting the error.
Damith Weerasinghe at 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
good
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.


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

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

  Print Answers RSS
0 OriginalGriff 430
1 Sergey Alexandrovich Kryukov 314
2 virusstorm 268
3 Richard Deeming 220
4 Dave Kreskowiak 200
0 OriginalGriff 1,623
1 Sergey Alexandrovich Kryukov 1,135
2 Abhinav S 701
3 Dave Kreskowiak 627
4 Suvendu Shekhar Giri 568


Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 26 Jun 2012
Copyright © CodeProject, 1999-2015
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