Microsoft KB Archive/827045

= BUG: All check boxes are cleared in a data-bound CheckedListBox control when rows are added to the DataTable control =

Article ID: 827045

Article Last Modified on 4/16/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-





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.



CAUSE
This problem occurs because the CheckedListBox control is not designed for data binding.



RESOLUTION
To 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(&quot;FirstName&quot;) = &quot;Russell&quot; row(&quot;LastName&quot;) = &quot;King&quot; '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[&quot;FirstName&quot;] = &quot;Russell&quot;; row[&quot;LastName&quot;] = &quot;King&quot;; //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.



Steps to Reproduce the Behavior
 Start Microsoft Visual Studio .NET.</li> Create a new Windows application by using Visual Basic .NET or Visual C# .NET. By default, Form1 is created.</li> Add a CheckedListBox control to Form1.</li> Right-click the CheckedListBox1 control, and then click Properties.</li> In the Properties dialog box, set the CheckOnClick property to True.</li> On the View menu, click Server Explorer.</li> In Server Explorer, right-click Data Connections, and then click Add Connection.</li> In the Data Link Properties dialog box, type or select the name of your computer that is running SQL Server in the Select or enter a server name box.

Note Type your SQL Server user name and password, if any, in the User name box and the Password box.</li> Select the Northwind database in the Select the database on the server list, and then click OK.</li> In Server Explorer, under the Data Connections section, expand your data connection.</li> Expand Tables, and then drag the Employees table to Form1.</li> Right-click SqlDataAdapter1, and then click Generate Dataset.</li> In the Generate Dataset dialog box, click OK.</li>  Add the following code to the Form1_Load event handler:

Visual Basic .NET Code 'Add rows to the dataset DataSet11. SqlDataAdapter1.Fill(DataSet11) 'Set the DataSource property of the CheckedListBox control to the Employees table. CheckedListBox1.DataSource = DataSet11.Tables(0) 'Set the DisplayMember property to the FirstName column. CheckedListBox1.DisplayMember = &quot;FirstName&quot; Visual C# .NET Code //Add rows to the dataset DataSet11. sqlDataAdapter1.Fill(dataSet11); //Set the DataSource property of the CheckedListBox control to the Employees table. checkedListBox1.DataSource = dataSet11.Tables[0]; //Set the DisplayMember property to the FirstName column. checkedListBox1.DisplayMember = &quot;FirstName&quot;; </li> Add a Button control to Form1.</li>  Add the following code to the Button1_Click event handler:

Visual Basic .NET Code '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(&quot;FirstName&quot;) = &quot;Russell&quot; row(&quot;LastName&quot;) = &quot;King&quot; 'Add the DataRow to the Employees table in DataSet11. DataSet11.Tables(0).Rows.Add(row) Visual C# .NET Code //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[&quot;FirstName&quot;] = &quot;Russell&quot;; row[&quot;LastName&quot;] = &quot;King&quot;; //Add the DataRow to the Employees table in DataSet11. dataSet11.Tables[0].Rows.Add(row); </li> On the Build menu, click Build Solution.</li> <li>On the Debug menu, click Start.</li> <li>Click to select some items in the CheckedListBox control.

Note When you click to select items in the CheckedListBox1 control, the check boxes are selected.</li> <li>Click Button1 to add a row. You may notice that the new item is added to the CheckedListBox control, but all the check boxes that are selected on the CheckedListBox control are cleared.</li></ol>

<div class="references_section">