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

#1 Guest_Brett*


Reputation:

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;
                                break;

                            case "PRE EXAM INFO":

                                blIncludeIssue = true;
                                break;

                            default:
// 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;

                                }

                                break;

                        }

                       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.

Brett

Is This A Good Question/Topic? 0

Replies To: DataGridView CheckBox check if ticked

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5428
  • View blog
  • Posts: 11,640
  • 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*


Reputation:

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:

((bool)obRow.Cells[2].Value)))



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

Page 1 of 1