BUG: The ListBox control or the ComboBox control copies list items several times in Visual Basic .NET or in Visual C# .NET (820636)
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)
SYMPTOMSYou have a Microsoft Windows form with a ListBox control or a ComboBox control. When you bind the ListBox or the ComboBox to a data source, the control copies the items in the list
several times.CAUSEWhen you create the ListBox or when you create the ComboBox, the form does not have a BindingContext property. The ListBox or the ComboBox must have a BindingContext to copy the data. Therefore, the control requests that the form
send the BindingContext. The form creates the BindingContext, and then the form fires notification to the control. The ListBox or the ComboBox receives the notification, and then the control tries to update
the DataManager property. Therefore, the ListBox or the ComboBox creates the first copy of the list. When the form creates the BindingContext, the ListBox or the ComboBox creates the second copy of the list.RESOLUTIONTo resolve this problem, use the following procedures:
- Set the BindingContext property of the
control before you set any data bindings.
- Set the DisplayMember property of the
control before you set the DataSource property.
To use these procedures, follow these steps:
- Replace the existing code in the Form f
constructor with the following code.
Microsoft Visual Basic .NET CodeDim pt As ProductTypes = New ProductTypes
'Add a ComboBox control to the form.
Dim cb As ComboBox = New ComboBox
Controls.Add(cb)
'Invoke the binding context for the ComboBox.
Dim bc As New BindingContext
cb.BindingContext = bc
'Set the DataSource property and set the DisplayMember property of the ComboBox.
cb.DisplayMember = "Description"
cb.DataSource = pt Microsoft Visual C# .NET CodeProductTypes pt = new ProductTypes();
//Add a ComboBox control to the form.
ComboBox cb = new ComboBox();
Controls.Add(cb);
//Invoke the binding context for the ComboBox.
BindingContext bc=new BindingContext();
cb.BindingContext =bc;
//Set the DataSource property and set the DisplayMember property of the ComboBox.
cb.DisplayMember = "Description";
cb.DataSource =pt; Note You can also use this code for a ListBox control
by replacing ComboBox with
ListBox. - On the Debug menu, click
Start.
The CopyTo method is called
only one time.
STATUSMicrosoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.REFERENCESFor more information, visit the following link in Microsoft
Developer Network (MSDN) Web site:
Modification Type: | Minor | Last Reviewed: | 2/3/2006 |
---|
Keywords: | kbvs2005swept kbvs2005doesnotapply kbvs2002sp1sweep kbDataBinding kbListBox kbCtrl kbControl kbComboBox kbbug KB820636 kbAudDeveloper |
---|
|