Bruce
2009-04-02 20:15:02 UTC
Greetings,
I am seeing an anomaly in the display of check box controls in a
DataGridView. I am able to programmatically set the check box state for each
row in the DataGridView. However, for the currently selected row the change
in state does not get reflected in the UI. Here is the relevant code that
sets up the controls:
DataGridView dataGridViewDocument = new DataGridView();
// Only allow one row in the DataGridView to be selected at a time.
dataGridViewDocument.MultiSelect = false;
. . .
// Add a check box column to the DataGridView by
// creating a DataColumn and adding it to the DataSet.
DataColumn dataColumn = new DataColumn();
dataColumn.DataType = System.Type.GetType("System.Boolean");
dataColumn.AllowDBNull = false;
dataColumn.Caption = "selected";
dataColumn.ColumnName = "CheckBox";
dataColumn.DefaultValue = 0;
// Add the data column to the data set before setting the data source.
dataSetSearchResults.Tables[FastDoc.FWHSearch].Columns.Add(dataColumn);
dataGridViewDocument.DataSource =
dataSetSearchResults.Tables[FastDoc.FWHSearch];
Here is code that manifests the problem:
bool IsToBeChecked = true;
foreach (DataGridViewRow dataGridViewRow in this.dataGridViewDocument.Rows)
{
dataGridViewRow.Cells["CheckBox"].Value = IsToBeChecked;
}
If all of the rows start off unchecked, after the loop iterates through all
of the rows, the check box will appear checked for all of the rows except for
the currently selected row. The opposite is true for starting off with all
checkboxes checked and attempting to clear all of the check boxes; after the
loop completes, all of the check boxes appear unchecked except for the
currently selected row.
Here is some additional information. When I run the program in the
debugger, I can confirm that the actual value of
dataGridViewRow.Cells["CheckBox"].Value gets correctly set to the value of
IsToBe checked. The change simply doesn't appear in the UI for the currently
selected row. If I change the state of the check box of the selected row by
manually clicking on it, the check box properly toggles between being checked
and unchecked.
As a test, I modified the code to temporarily unselect and reselect the
currently selected row:
if (dataGridViewRow.Cells["CheckBox"].Selected)
{
dataGridViewRow.Cells["CheckBox"].Selected = false;
dataGridViewRow.Cells["CheckBox"].Value = IsToBeChecked;
dataGridViewRow.Cells["CheckBox"].Selected = true;
}
This change caused the bug to go away. The check boxes in all rows
including the currently selected row were updated in the UI with the correct
state; however, this is not a good workaround in that the act of unselecting
and reselecting a row causes the OnSelectionChanged event handler for the
DataGridView to run with undesirable side effects. I could set a global flag
and modify the event handler to detect when it has been triggered by the
above code and skip processing but that is messy. I would rather just get
the code that updates the check box to update the UI as expected.
I am running Visual Studio 2008 SP1 with the .NET Framework 3.5 SP1.
Does anyone have any ideas as to why it might be happening or have any ideas
how to fix it? Thanks.
-Bruce
I am seeing an anomaly in the display of check box controls in a
DataGridView. I am able to programmatically set the check box state for each
row in the DataGridView. However, for the currently selected row the change
in state does not get reflected in the UI. Here is the relevant code that
sets up the controls:
DataGridView dataGridViewDocument = new DataGridView();
// Only allow one row in the DataGridView to be selected at a time.
dataGridViewDocument.MultiSelect = false;
. . .
// Add a check box column to the DataGridView by
// creating a DataColumn and adding it to the DataSet.
DataColumn dataColumn = new DataColumn();
dataColumn.DataType = System.Type.GetType("System.Boolean");
dataColumn.AllowDBNull = false;
dataColumn.Caption = "selected";
dataColumn.ColumnName = "CheckBox";
dataColumn.DefaultValue = 0;
// Add the data column to the data set before setting the data source.
dataSetSearchResults.Tables[FastDoc.FWHSearch].Columns.Add(dataColumn);
dataGridViewDocument.DataSource =
dataSetSearchResults.Tables[FastDoc.FWHSearch];
Here is code that manifests the problem:
bool IsToBeChecked = true;
foreach (DataGridViewRow dataGridViewRow in this.dataGridViewDocument.Rows)
{
dataGridViewRow.Cells["CheckBox"].Value = IsToBeChecked;
}
If all of the rows start off unchecked, after the loop iterates through all
of the rows, the check box will appear checked for all of the rows except for
the currently selected row. The opposite is true for starting off with all
checkboxes checked and attempting to clear all of the check boxes; after the
loop completes, all of the check boxes appear unchecked except for the
currently selected row.
Here is some additional information. When I run the program in the
debugger, I can confirm that the actual value of
dataGridViewRow.Cells["CheckBox"].Value gets correctly set to the value of
IsToBe checked. The change simply doesn't appear in the UI for the currently
selected row. If I change the state of the check box of the selected row by
manually clicking on it, the check box properly toggles between being checked
and unchecked.
As a test, I modified the code to temporarily unselect and reselect the
currently selected row:
if (dataGridViewRow.Cells["CheckBox"].Selected)
{
dataGridViewRow.Cells["CheckBox"].Selected = false;
dataGridViewRow.Cells["CheckBox"].Value = IsToBeChecked;
dataGridViewRow.Cells["CheckBox"].Selected = true;
}
This change caused the bug to go away. The check boxes in all rows
including the currently selected row were updated in the UI with the correct
state; however, this is not a good workaround in that the act of unselecting
and reselecting a row causes the OnSelectionChanged event handler for the
DataGridView to run with undesirable side effects. I could set a global flag
and modify the event handler to detect when it has been triggered by the
above code and skip processing but that is messy. I would rather just get
the code that updates the check box to update the UI as expected.
I am running Visual Studio 2008 SP1 with the .NET Framework 3.5 SP1.
Does anyone have any ideas as to why it might be happening or have any ideas
how to fix it? Thanks.
-Bruce