Lior
Lior

Reputation: 149

CheckBox value in DataGridView is always true

I have a WinForm application that has DataGridView with one column with CheckBox. After selecting few rows with the checkbox I need to iterate rows and check if the CheckBox is ticked or not.

I have tried woth for and foreach loop but everytime the value of the bx is true! What's funny is that I have a button that select all and another that's clearing all using the code, and it works!

My code for creating the DataGridView:

 ComputerSelection computerSelection = new ComputerSelection();
            DataGridViewCheckBoxColumn checkBox = new DataGridViewCheckBoxColumn();
            checkBox.ValueType = typeof(bool);
            checkBox.Name = "CheckBox";
            checkBox.HeaderText = "Select";
            computerSelection.compGridView.Columns.Add(checkBox);
            computerSelection.compGridView.Columns.Add("Name", "Name");
            computerSelection.compGridView.Columns.Add("AgentVersion", "Agent Version");
            computerSelection.compGridView.Columns.Add("Status", "Status");
            computerSelection.compGridView.Columns.Add("Domain", "Domain");

Code for iterating (that most of the posts that i searched sharing that solution as the correct one):

 foreach (DataGridViewRow row in computerSelection.compGridView.Rows)
            {
                if ((bool)row.Cells["CheckBox"].Value)
                {
                    computerSelection.ComputersList.Add(row.Cells[1].Value.ToString());
                }
            }

This code ALWAYS return TRUE even on an unchecked checkboxes. I searched so much posts on StackOverFlow, even tried to use the as DataGridViewCheckBoxCell but without success.

the selecting all button code (That uses the same mechanism :():

 private void SelectAllButton_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < compGridView.Rows.Count; i++)
            {
                compGridView.Rows[i].Cells[0].Value = true;
            }
        }

I need that after every iteration the "row.Cells[1].Value.ToString()" code will return false or true, not always true.

Upvotes: 0

Views: 3946

Answers (2)

Lior
Lior

Reputation: 149

I found the way to do it! Used the CellContentClick Event and then with the EditedFormattedValue attribute, the bool value is REAL this time.

 private void compGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                if ((bool)compGridView.Rows[e.RowIndex].Cells[0].EditedFormattedValue)
                {
                    ComputersList.Add(compGridView.Rows[e.RowIndex].Cells[1].Value.ToString());
                }
                else
                {
                    ComputersList.Remove(compGridView.Rows[e.RowIndex].Cells[1].Value.ToString());
                }
            }
        }

Thank you

EDIT: Figured out why in the first place I had that issue, That's even a little bit funny. I had an event that when closing the form, the value of every checkbox turns into TRUE!

 private void ComputerSelection_FormClosed(object sender, FormClosedEventArgs e)
        {
            for(int i = 0; i < compGridView.Rows.Count; i++)
            {
                compGridView.Rows[i].Cells[0].Value = true;
            }
        }

Upvotes: 2

Scott Mallon
Scott Mallon

Reputation: 136

You need to find your control and cast it as a checkbox object and see if it is checked. The Checked attribute is where checkboxes store true/false values for whether or not they are checked. Right now you are just checking a cell for data to see if it has any value and it is returning true every time. The way I'm doing it below is how you would do it with an ASP.NET Webforms GridView object. I assume it's largely the same idea for Windows Forms, it's just that DataGridView might have a different method for "finding your control" other than the FindControl method you use in ASP.NET Webforms for a GridView. But I'd bet it's likely the same.

I'm out and about and can't test this exact functionality with Windows Forms, but the idea behind what you need to do to make your logic work, however, is exactly the same.

Change your condition to something like this:

foreach (DataGridViewRow row in computerSelection.compGridView.Rows)
        {
            // Don't cast as bool. Convert to Checkbox object and see if it is checked. 
            if (((CheckBox)row.FindControl("CheckBox")).Checked) // row.FindControl might be different but might be the same for Winforms.
            {
                computerSelection.ComputersList.Add(row.Cells[1].Value.ToString());
            }
        }

Upvotes: 1

Related Questions