How to Sort ComboBox that has a DataSource?

  • Thread starter Thread starter RamChip
  • Start date Start date
R

RamChip

VB.net... MS Access data files

Got 2 Comboboxes. Both are bound to a DataSet. One lists a groups of
numbers. The other associated names.

The current method is to list the Combobox with numbers (which they are in
numeric order in the Access data file).

What I want to accomplish is to give a user the method to toggle the
associated names to be sorted or not (not at startup).

When I try to Sort the Combobox of associated names (combobox2) I get the
error:

Can't sort a ComboBox that has a DataSource set. Sort the data using the
underlying data model.

How can this be accomplished?

Many thanks

Ralph
 
¤ VB.net... MS Access data files
¤
¤ Got 2 Comboboxes. Both are bound to a DataSet. One lists a groups of
¤ numbers. The other associated names.
¤
¤ The current method is to list the Combobox with numbers (which they are in
¤ numeric order in the Access data file).
¤
¤ What I want to accomplish is to give a user the method to toggle the
¤ associated names to be sorted or not (not at startup).
¤
¤ When I try to Sort the Combobox of associated names (combobox2) I get the
¤ error:
¤
¤ Can't sort a ComboBox that has a DataSource set. Sort the data using the
¤ underlying data model.
¤
¤ How can this be accomplished?

What it is saying is that you should perform your sort when you query the database so that your
associated names are in order.


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
What it is saying is that you should perform your sort when you query the database so that your
associated names are in order.

So in order for me to have both an option to view a sorted number Combobox and
a names combobox I'll need to have 2 datasets? Oh well... okay. Kinda knew I
could do that... but hoped that there was a way to do it via code.

Thanks!
 
¤ With Deft Fingers, Paul Clement <[email protected]>
¤ wrote:
¤
¤ >What it is saying is that you should perform your sort when you query the database so that your
¤ >associated names are in order.
¤
¤ So in order for me to have both an option to view a sorted number Combobox and
¤ a names combobox I'll need to have 2 datasets? Oh well... okay. Kinda knew I
¤ could do that... but hoped that there was a way to do it via code.

The only other way I can think of is to use an Array as the DataSource which would of course require
the additional step of getting your data into the array.


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
Actually you could accomplish this if you create 2 DataView objects from the DataTable and sort each one as you like then bind each DataView object to the ComboBox
desired. The following demonstrates how to do it:

Dim da1 As SqlDataAdapter = New SqlDataAdapter("select * from Customers", cn)
da1.Fill(ds, "customers")

Dim dv1 As DataView = New DataView(ds.Tables("Customers"))
Dim dv2 As DataView = New DataView(ds.Tables("Customers"))

dv1.Sort = "CustomerID"
ComboBox1.DataSource = dv1
ComboBox1.DisplayMember = "CustomerID"

dv2.Sort = "CompanyName"
ComboBox2.DataSource = dv2
ComboBox2.DisplayMember = "CompanyName"

I hope this helps!


Thanks,
Hussein Abuthuraya
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? For information about the Microsoft Strategic Technology Protection Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.
 
Actually you could accomplish this if you create 2 DataView objects from the DataTable and sort each one as you like then bind each DataView object to the ComboBox
desired. The following demonstrates how to do it:
I hope this helps!

Thanks. Yes it does. Actually I was (eventually) going to just create
another DataAdapter (for sorting via names). Then toggle between my original
DataAdapter (which is the default sort by numbers) and the 2nd DA.

But I'll try your method (always looking for better coding methods).

Thanks!
 
Back
Top