BUG: All check boxes are cleared in a data-bound CheckedListBox control when rows are added to the DataTable control (827045)
The information in this article applies to:
- Microsoft Visual Basic .NET (2003)
- Microsoft Visual Basic .NET (2002)
- Microsoft Visual C# .NET (2003)
- Microsoft Visual C# .NET (2002)
SYMPTOMS You have a data-bound CheckedListBox
control on your Windows Form. When you click to select some items in the
CheckedListBox control at runtime and then you add a new row
to the DataTable control that is bound to the
CheckedListBox control, all the check boxes on the control are
cleared.
Note Check boxes are not cleared when the
CheckedListBox control is not data-bound. CAUSEThis problem occurs because the
CheckedListBox control is not designed for data binding.
RESOLUTIONTo resolve this problem, follow these steps:
- Replace the existing code in the
Button1_Click event handler with the following code:
Microsoft Visual Basic .NET Code'Define a DataRowView array.
Dim oCheckedItems As DataRowView()
ReDim oCheckedItems(CheckedListBox1.CheckedItems.Count - 1)
'Copy the checked items in CheckedListBox1 to the DataRowView array.
CheckedListBox1.CheckedItems.CopyTo(oCheckedItems, 0)
'Create a new DataRow with the same schema as the Employees table.
Dim row As DataRow = DataSet11.Tables(0).NewRow
'Allocate data to the FirstName, the LastName, and the rowguid columns.
row("FirstName") = "Russell"
row("LastName") = "King"
'Add the DataRow to the Employees table in DataSet11.
DataSet11.Tables(0).Rows.Add(row)
Dim x As Integer
For x = LBound(oCheckedItems) To UBound(oCheckedItems)
'Item(2) (that is, the FirstName column in the oCheckedItems array) is the display member.
'Find whether the item is present in the oCheckedItems array.
'When the item is present, set the checked state of the item in the CheckedListBox control to true.
CheckedListBox1.SetItemChecked(CheckedListBox1.FindStringExact(oCheckedItems(x).Item(2).ToString, -1), True)
Next Microsoft Visual C# .NET Code//Define a DataRowView array.
DataRowView []oCheckedItems= new DataRowView[checkedListBox1.CheckedItems.Count];
//Copy the checked items in checkedListBox1 to the DataRowView array.
checkedListBox1.CheckedItems.CopyTo(oCheckedItems, 0);
//Create a new DataRow with the same schema as the Employees table.
DataRow row = dataSet11.Tables[0].NewRow();
//Allocate data to the FirstName, the LastName, and the rowguid columns.
row["FirstName"] = "Russell";
row["LastName"] = "King";
//Add the DataRow to the Employees table in dataSet11.
dataSet11.Tables[0].Rows.Add(row);
int x;
for( x =oCheckedItems.GetLowerBound(0) ;x<= oCheckedItems.GetUpperBound(0) ;x++)
{
//The FirstName column in the oCheckedItems array is the display member.
//Determine if the item is present in the oCheckedItems array.
//When the item is present, set the checked state of the item in the CheckedListBox control to true.
checkedListBox1.SetItemChecked(checkedListBox1.FindStringExact(oCheckedItems[x][2].ToString(), -1), true);
}
- On the Build menu, click Build
Solution.
- On the Debug menu, click
Start.
STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are
listed at the beginning of this article.
REFERENCES For additional
information, click the following article number to view the article in the
Microsoft Knowledge Base: 327896
BUG: CheckedListBox Control Loses Check Marks When the Visible
Property Is Changed to FALSE and Then Back to TRUE
For more information, visit the following
Microsoft Developer Network (MSDN) Web site:
Modification Type: | Minor | Last Reviewed: | 2/9/2006 |
---|
Keywords: | kbvs2005swept kbvs2005doesnotapply kbvs2002sp1sweep kbpending kbtable kbWindowsForms kbForms kbDataBinding kbCtrl kbControl kbbug KB827045 kbAudDeveloper |
---|
|