Solved

C# loop thru DataGridView with checkbox column

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET 5 Templates 2 81
Counting connections to SQL Server through C# 3 34
Close form "before" open 3 43
Jquery/JSON passing value from one function to another 12 41
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

825 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