Click here to Skip to main content
12,634,407 members (25,223 online)
Rate this:
 
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 26-Jun-12 0:16am
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?
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 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
 
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<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)
         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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161208.2 | Last Updated 26 Jun 2012
Copyright © CodeProject, 1999-2016
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