Allstar
Allstar

Reputation: 439

Unbind datasource after populating combobox

basic question - is it possible to populate a combobox with datasource and then cut the link to the datasource, but keep the data in the combobox? this will enable you to reuse your ds.Tables[0] below without affecting the first populated combobox.

comboBox1.DataSource = ds.Tables[0];

(The ds is populated from MS SQL Server with SqlDataAdapter)

Upvotes: 1

Views: 208

Answers (1)

Steve
Steve

Reputation: 216363

No, it is not possible without copying the source data in some way. The DataSource property is a reference to your dataset, datatable or whatever object not a copy of it. Setting DataSource to null will remove any possibilities for the combo to see the data referenced.

Instead you could easily create a copy of your original table using the appropriate method available in the DataTable class

 comboBox1.DataSource = ds.Tables[0].Copy();

This create a new copy of the table with the actual structure and content, but it is a copy in another memory area of the informations stored in the first object. At this point you have two distinct objects in memory and you can change the first one without affecting the second.

Let me say also that this is not very smart with big tables. Don't use this approach if your table contains a lot of records for obvious reasons.

Upvotes: 1

Related Questions