2 Replies - 3232 Views - Last Post: 24 February 2011 - 08:31 AM Rate Topic: -----

#1 Guest_Brett*


DataGridView CheckBox check if ticked

Posted 24 February 2011 - 08:01 AM

Hi Hopefully someone can help me please. I have a dataGridView control, and column 3 is a checkBox.

I am trying to use the checkbox to control what is output to excel on the press of a button. I make a selection from a ComboBox and it populates my data GridView control as standard, based on the selection made. The user can then review the DataGridView control and untick some of it contents, therefore limiting what is output when the button is pressed to discount some data if required.

Here is my code:

foreach (XmlNode issue in issueslist)

     string issueCode = issue.Attributes.GetNamedItem("id").Value.ToString();
     XmlNode code = xmlDoc.SelectSingleNode("modsoft/issue_list/issues[@id='" + issueCode + "']");
     string szDataset = code.SelectSingleNode("dataset").InnerText;
     string szType = code.SelectSingleNode("type").InnerText;

     bool blIncludeIssue = false;
     foreach (DataGridViewRow obRow in dataGridView1.Rows)
                        switch (szType)

                            case "NO SUPPORT":

                                blIncludeIssue = true;

                            case "PRE EXAM INFO":

                                blIncludeIssue = true;

// This is where I want to check my checkbox value.  If my obRow Cell [0] matches szDataset and
//  obRows.Cells[2] (My checkbox is ticked it should return true, but when I untick the check box it 
// still outputs the data it is supposed to restrict.

      if ((obRow.Cells[0].Value.ToString() == szDataset) && (obRow.Cells[2].Value!=null))
                                    blIncludeIssue = true;




                       if (blIncludeIssue) break;


                    if (blIncludeIssue)
//This is some of the code that should run for each entry where my if statement above is true

                        ws.Cells[1, 2] = label5.Text;
                        ws.Cells[1, 3] = maskedTextBox1.Text;
                        ((Range)ws.Cells[1, 2]).NumberFormat = "@";
                        ws.Cells[1, 4] = label6.Text + textBox2.Text;
                        ws.Cells[1, 6] = "Viewed: " + DateTime.Now;

Thank you in advance.


Is This A Good Question/Topic? 0

Replies To: DataGridView CheckBox check if ticked

#2 tlhIn`toq  Icon User is offline

  • Left site for a while
  • member icon

Reputation: 5926
  • View blog
  • Posts: 12,921
  • Joined: 02-June 10

Re: DataGridView CheckBox check if ticked

Posted 24 February 2011 - 08:20 AM

Everything you do is based on

blIncludeIssue = true;

No place do you set this boolean to false, so yeah, it is always true and thus always produces output.

If the checkbox is not checked, set this bool to false.
Was This Post Helpful? 1
  • +
  • -

#3 Guest_Brett*


Re: DataGridView CheckBox check if ticked

Posted 24 February 2011 - 08:31 AM

Thank you,

This appears to have solved it.

if ((obRow.Cells[0].Value.ToString() == szDataset) && ((obRow.Cells[2].Value!=null) && ((bool)obRow.Cells[2].Value)))

I believe it is because the checkbox can be null or false, so it evaluates for null and it value now.

My Colleague added the extra code below:


So I think its something to do with that !!
Was This Post Helpful? 0

Page 1 of 1