Neil
Neil

Reputation: 11919

How can I tick or untick a checkbox in a datagridview using White

I have a WinForms app, that displays a DataGridView. It is automatically populated from a BindingSource, and contains several rows of data. The columns include the standard things like strings. Some of these columns are CheckBoxes.

Name | User | Admin ---- ---- ----- John | X |
Fred | | X

I am writing some automated tests using TestStack.White. I can read the existing state of the CheckBoxes without issue.

How do I set or clear the checkboxes? I've tried:

  1. Table.Rows[0].Cells[1].Value = true;

Fails because the underlying row/cell is deleted before White can read back the current value.

And also:

  1. Table.Rows[0].Cells[1].SetValue(true);

Fails to set the new value.

Upvotes: 5

Views: 2074

Answers (5)

sarh
sarh

Reputation: 6647

I also could not extract checkbox that belongs to datagridviewcheckboxcolumn neither from cell nor from row, and finally ended up with a hack using mouse, something like this:

Rect bounds = table.Rows[rowNo].Cells[colNo].Bounds;
Point center = new Point(bounds.X + bounds.Width / 2, bounds.Y + bounds.Height / 2);
Mouse.Instance.Click(center);

Upvotes: 0

Jeremy Thompson
Jeremy Thompson

Reputation: 65712

Because TestStack.White cannot access the underlying object model you will have to come up with a New button or Reset button. Or some GUI way (with logic) to clear the Checkbox values by setting the BindingSource's boolean fields to false.

Here is some psuedo code to illustrate.

private void New_Click(object sender, System.EventArgs e) 
{
   DataTable dt = (DataTable)dgv.DataSource;
   dt[1]["IsAdmin"].value = false;
   //In ASP.Net you also need a dgv.Bind(); //this is not necessary in winforms
}

There should already be a way in your system to test with Checkboxes cleared, it should be consistent with how the end user currently do it.

Upvotes: 1

codeDom
codeDom

Reputation: 1779

Because your DataGridView is bound to BindingSource, you should change the Boolean value in BindingSource rather than through the dataGridView control.

Upvotes: 0

Sanju
Sanju

Reputation: 1518

I believe the check boxes in c# has a property 'Checked'. And even though it's in the DataGridView, I believe the property of the controls remains the same.

So, can you try this instead

Table.Rows[0].Cells[1].Checked = true;

Resources: http://www.c-sharpcorner.com/uploadfile/mahesh/checkbox-in-C-Sharp3/

Upvotes: 0

M. Schena
M. Schena

Reputation: 2107

My this be an helpful way to set values? DataGridView checkbox column - value and functionality:

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    row.Cells[CheckBoxColumn1.Name].Value = true;
}

Or what is your Table for an Object?

Upvotes: 0

Related Questions