Solved

C# loop thru DataGridView with checkbox column

Posted on 2011-09-22
3
1,648 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 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now