I have a method which select rows in datagrid and change value of checkboxcolumn:
foreach (DataGridViewRow gridRow in dataGridView1.Rows)
{
if (_selectedIDs != null)
foreach (long id in _selectedIDs)
{
gridRow.Selected = false;
if ((long)((DataRowView)gridRow.DataBoundItem)["ObjectD"] == id)
gridRow.Selected = true;
}
if (_checkedIDs != null)
foreach (long id in _checkedIDs)
{
((DataRowView)gridRow.DataBoundItem)["Choosen"] = 0;
if ((long)((DataRowView)gridRow.DataBoundItem)["ObjectD"] == id)
((DataRowView)gridRow.DataBoundItem)["Choosen"]=true;
}
}
dataGridView1.Refresh();
When I’m debugging this code I see that it enters in lines
gridRow.Selected = true;
and
((DataRowView)gridRow.DataBoundItem)["Choosen"]=true;
and in quickwatch I see that properties of those rows are changed.
But after execution of this code I still have only one row selected
Does anyone have an idea what’s wrong with this code?
In addition to the MultiSelect, you might want to make sure that the class you databind implements the
INotifyPropertyChangedinterface.You also could set the
selected = falsebefore you start looping, or you will overwrite your selection every time…