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.

CAUSE

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

RESOLUTION

To resolve this problem, follow these steps:
  1. 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);
    }
       
  2. On the Build menu, click Build Solution.
  3. 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.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Start Microsoft Visual Studio .NET.
  2. Create a new Windows application by using Visual Basic .NET or Visual C# .NET. By default, Form1 is created.
  3. Add a CheckedListBox control to Form1.
  4. Right-click the CheckedListBox1 control, and then click Properties.
  5. In the Properties dialog box, set the CheckOnClick property to True.
  6. On the View menu, click Server Explorer.
  7. In Server Explorer, right-click Data Connections, and then click Add Connection.
  8. 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.
  9. Select the Northwind database in the Select the database on the server list, and then click OK.
  10. In Server Explorer, under the Data Connections section, expand your data connection.
  11. Expand Tables, and then drag the Employees table to Form1.
  12. Right-click SqlDataAdapter1, and then click Generate Dataset.
  13. In the Generate Dataset dialog box, click OK.
  14. 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 = "FirstName"
    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 = "FirstName";
  15. Add a Button control to Form1.
  16. 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("FirstName") = "Russell"
    row("LastName") = "King"
    '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["FirstName"] = "Russell";
    row["LastName"] = "King";
    //Add the DataRow to the Employees table in DataSet11.
    dataSet11.Tables[0].Rows.Add(row);
  17. On the Build menu, click Build Solution.
  18. On the Debug menu, click Start.
  19. 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.
  20. 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.

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:MinorLast Reviewed:2/9/2006
Keywords:kbvs2005swept kbvs2005doesnotapply kbvs2002sp1sweep kbpending kbtable kbWindowsForms kbForms kbDataBinding kbCtrl kbControl kbbug KB827045 kbAudDeveloper