Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# Sorted ListBox bound to a database throws exception

Status
Not open for further replies.

polocar

Programmer
Sep 20, 2004
89
IT
Hi,
I noticed a strange behaviour of the ListBox control in C# when I bind it to an Access database: if I try to set its "Sorted" property to true, the exception "Impossible to modify Items collection when DataSource property is set" is thrown during the execution.

The strange thing is that, if I replace the ListBox with a ComboBox control, the program functions correctly (I can set its Sorted property to true without exceptions).

Is it a bug or there is a reason for this different behaviour between ListBox and ComboBox?

Thank you very much

P.S.: These are the statements regarding my problem:

Code:
cboCities.Parent = this;
cboCities.Size = new Size(100, 30);
cboCities.Location = new Point(10, 10);
cboCities.Sorted = true;
cboCities.DataSource = dtCities;  // DataTable object
cboCities.ValueMember = "IDCity";      // 1st field of the table
cboCities.DisplayMember = "NameCity";  // 2nd field of the table

lstCities.Parent = this;
lstCities.Size = new Size(100, 100);
lstCities.Location = new Point(cboCities.Left + cboCities.Width + 10, cboCities.Top);

// If you remove the comment to the next statement,
// an exception will be thrown during execution
// lstCities.Sorted = true;
 
lstCities.DataSource = dtCities;   // DataTable object
lstCities.ValueMember = "IDCity";     // 1st field of the table
lstCities.DisplayMember = "NameCity"; // 2nd field of the table
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top