One datasource, two combo boxes, two different sorts

D

Diane Yocom

I'm looking for ideas on how to populate two different combo boxes with one
datasource that is sorted in two different ways. The combo boxes will use
the same field for the value field, but display different fields. I'd like
the combo boxes to sort on their display field.

Requerying the database to create a second datasource is not an option.

I've tried cloning the datatable so I can sort the defaultview two different
ways, but datatable.clone is only a shallow clone, so I don't get the rows.

Any other suggestions?
Diane Y.
 
M

Morten Wennevik [C# MVP]

I'm looking for ideas on how to populate two different combo boxes with one
datasource that is sorted in two different ways. The combo boxes will use
the same field for the value field, but display different fields. I'd like
the combo boxes to sort on their display field.

Requerying the database to create a second datasource is not an option.

I've tried cloning the datatable so I can sort the defaultview two different
ways, but datatable.clone is only a shallow clone, so I don't get the rows.

Any other suggestions?
Diane Y.

Hi Diane,

You can do this with a DataSource, but I don't think you can do this with a DataSet/DataTable as DataSource. Having each combobox sort differently might indicate the relationship between the fields are not necessary, in which case you could easily create two lists instead and have the selected item map to some object.
 
J

Jack Jackson

I'm looking for ideas on how to populate two different combo boxes with one
datasource that is sorted in two different ways. The combo boxes will use
the same field for the value field, but display different fields. I'd like
the combo boxes to sort on their display field.

Requerying the database to create a second datasource is not an option.

I've tried cloning the datatable so I can sort the defaultview two different
ways, but datatable.clone is only a shallow clone, so I don't get the rows.

There is a DataView between the DataTable and the bound control. By
default this is a DataView supplied by the DataTable
(DataTable.DefaultView). The sorting is done in the DataView.

You can create another DataView and use it for the second combo box.
There is an example in the MSDN documentation for DataView at
<http://msdn2.microsoft.com/en-us/library/system.data.dataview.aspx>.
 
D

Diane Yocom

I tried creating a new dataview with the following line:

Dim dvSorted as new dataview(mydataset, "", "SortFieldName",
DataViewRowState.None)

I then set the datasource of one of the combo boxes to dvSorted.
Unfortunately, it's not sorted by the field that I specified. It's sorted
the same way it came out of the database.

What am I missing here?
 
D

Diane Yocom

Correction, my line of code is this:

Dim dvSorted as new dataview(mydataset.tables(0), "", "SortFieldName",
DataViewRowState.None)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top