Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 394
Neil Avatar asked Oct 31 '22 10:10

Neil


2 Answers

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

like image 59
google dev Avatar answered Nov 15 '22 06:11

google dev


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.

like image 45
Jeremy Thompson Avatar answered Nov 15 '22 05:11

Jeremy Thompson