Possible Duplicate:
DataGridViewCheckBoxCell how to show checked when set during form load
I am having a problem with a DataGridView in a C#-WinForms project.
I fill it with data from a local database. Additionally, I have added a CheckBoxColumn that isn't bound to any data. I use this to filter a ListBox depending on the checked rows. That all is placed on the second page of a TabControl on a dialog form and is working perfectly.
Now I want to programatically select rows based on on a list of strings that I pass through when opening the form. I tried this code:
foreach (string pre in preselect)
{
foreach (DataGridViewRow row in dgvProtFunc.Rows)
{
if ((string)row.Cells[2].Value == pre)
{
((DataGridViewCheckBoxCell)row.Cells[0]).Value = true;
}
}
}
Setting the values and then the triggered filtering works on formload. However, the checkboxes are not checked. When checking one, all other values are lost. Thats bad, since I have to use such a pre-select. These are the other DataGridView event handler:
private void dgvProtFunc_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dgvProtFunc.IsCurrentCellDirty)
{
dgvProtFunc.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
private void dgvProtFunc_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (!FormLoadCheck)
{
string data = string.Empty;
ptSelectList.Clear();
foreach (DataGridViewRow row in dgvProtFunc.Rows)
{
if (row.Cells[0].Value != null && Convert.ToBoolean(row.Cells[0].Value) == true)
{
ptSelectList.Add(Convert.ToInt32(row.Cells[1].Value));
}
data += row.Cells[0].Value;
}
ptSelectList.Sort();
FilterRelais(ptSelectList.ToArray());
MessageBox.Show(data);
}
}
Can anyone explain this behaviour? Any solutions?