In this article I will provide solution to the following exception (error) which occurs when adding a Default (Blank) Item to ComboBox after it is populated with some DataSource in Windows Forms (WinForms) Application using C# and VB.Net.
Items collection cannot be modified when the DataSource property is set.
 
 
The Error
In the following code, first the ComboBox is supplied with a DataSource i.e. a DataTable and then a Default (Blank) Item is being inserted in it at first position.
ComboBox: Items collection cannot be modified when the DataSource property is set.
 
 
The Cause
The above action is not valid with ComboBox and the rule is that once a ComboBox is populated with a DataSource its collection cannot be modified and hence the above error (exception) is raised.
 
 
Solution
The solution is to insert a Default (Blank) Item to the DataSource itself i.e. the DataTable and then bind it to the ComboBox.
C#
//Fill the DataTable with records from Table.
DataTable dt = new DataTable();
sda.Fill(dt);
 
//Insert the Default Item to DataTable.
DataRow row = dt.NewRow();
row[0] = 0;
row[1] = "Please select";
dt.Rows.InsertAt(row, 0);
 
//Assign DataTable as DataSource.
cbCustomers.DataSource = dt;
cbCustomers.DisplayMember = "Name";
cbCustomers.ValueMember = "CustomerId";
 
VB.Net
'Fill the DataTable with records from Table.
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
 
'Insert the Default Item to DataTable.
Dim row As DataRow = dt.NewRow()
row(0) = 0
row(1) = "Please select"
dt.Rows.InsertAt(row, 0)
 
'Assign DataTable as DataSource.
cbCustomers.DataSource = dt
cbCustomers.DisplayMember = "Name"
cbCustomers.ValueMember = "CustomerId"
 
 
Reference
For reference, the complete article with explanation and source code on binding ComboBox from database with Default (Blank) Item is available here.