Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# loop thru DataGridView with checkbox column

Posted on 2011-09-22
3
Medium Priority
?
1,775 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
3 Comments
 
LVL 9

Accepted Solution

by:
xav056 earned 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

885 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