Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# loop thru DataGridView with checkbox column

Posted on 2011-09-22
3
Medium Priority
?
1,755 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 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

715 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