Solved

C# loop thru DataGridView with checkbox column

Posted on 2011-09-22
3
1,739 Views
Last Modified: 2012-05-12
Hello experts,

I have a DataGridView on my main form that I bind with a datatable.  

...
//bind to datagrid view
dgXpath.DataSource = myDataTable;
dgXpath.ReadOnly = false;
...

The three columns are of type:
CheckBox, Text, Text

After the user checks a checkbox they can click the Export button.  This will need to loop through the DataGridView and save the data from each selected row to a datatable for further processing.

I am trying to check the value of the cell of the CheckBox column.  If the cell is true than the checkbox was checked, so then get the values for the corrsponding columns of the same row.  

Problem/Question is:  
When I do this:
if (dr.Cells[0].Value != false)

Visual Studio is returning an error:
Cannot implicitly convert type 'object' to 'bool'. An explicit conversion exists (are you missing a cast?)

I am sure this is something simple but any help much appreciated.  Thanks!

       private void btnExport_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dr in dgXpath.Rows)    
            {

                if (dr.Cells[0].Value != false)
       
                {
                    MessageBox.Show("Rows " + dr.Index + " 3rd column is : " + dr.Cells[2].Value);
                    //save to datatable here
                }      
            }
        }

Thanks!
0
Comment
Question by:j420exe1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 9

Accepted Solution

by:
xav056 earned 500 total points
ID: 36583060
change this dr.Cells[0].Value!=false
to Convert.ToBoolean(dr.Cells[0].Value)!=false
0
 
LVL 4

Expert Comment

by:theHollow
ID: 36583253
Hello
I would place a breakpoint on the bracket beneath the "foreach".

Then, I would check the type of dr.Cells[0].Value
You can do that by going to the Immediate-window and type:

?dr.Cells[0].Value.GetType()

If this is a Boolean, you can cast it to Boolean and you have the value.
Like :
 - Convert.ToBoolean(dr.Cells[0].Value)
 - Boolean.Parse(dr.Cells[0].Value)
 - Boolean.TryParse(dr.Cells[0].Value, out boolVal)
 - (Boolean)dr.Cells[0].Value

But...
If this is a control (like a CheckBox), you need to cast dr.Cells[0].Value to CheckBox and then use the "Checked" property.

Hope that helps!
0
 

Author Closing Comment

by:j420exe1
ID: 36583284
That worked...thanks!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question