ListBox Control: DataSource, DisplayMember, ValueMember properties

  • Thread starter Thread starter JMe9ka
  • Start date Start date
J

JMe9ka

I have a ListBox to which I am assigning a DataTable as a DataSource, as
shown below.

=======================================================
DataTable VendorData = new DataTable();

// Code here retrieves Data and populates
// the VendorData DataTable.


listBox_Vendor.DataSource = VendorData;
listBox_Vendor.ValueMember = "ID";
listBox_Vendor.DisplayMember = "Name";
=======================================================

This code works fine when there is only one entry in the DataSource.
When the number of entries is greater than one, however, it throws the
System.ArgumentException below:

=======================================================
System.ArgumentException: Cannot modify the Items collection when the
DataSource property is set.

at System.Windows.Forms.ListBox.CheckNoDataSource()
at System.Windows.Forms.ListBox.Sort()
at System.Windows.Forms.ListBox.EndUpdate()
at System.Windows.Forms.ListBox.SetItemsCore(IList value)
at System.Windows.Forms.ListControl.DataManager_ItemChanged(Object
sender, ItemChangedEventArgs e)
at System.Windows.Forms.ListControl.SetDataConnection(Object
newDataSource, BindingMemberInfo newDisplayMember, Boolean force)
at System.Windows.Forms.ListControl.set_ValueMember(String value)
=======================================================

I don't quite understand what's going on here. There is no editing done
to the items of the ListBox anywhere.

Any assistance is immensely appreciated.


- J
 
Back
Top