sorting a combobox

G

Guest

Hello
I use the following code to populate a combobox

Me.cbxPeerReview.DropDownStyle = ComboBoxStyle.DropDownLis
With cbxPeerRevie
.DataSource = dtEm
.DisplayMember = "STR_NAME_F
.ValueMember = "ID_USER
End Wit
Me.cbxPeerReview.DroppedDown = Tru
Me.cbxPeerReview.SelectedIndex = -

I want to sort to combobox by the "STR_NAME_F", but if I add the .sorted = true line, I get the following error

An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dl
Additional information: Cannot sort a ComboBox that has a DataSource set. Sort the data using the underlying data model

How do I sort the data using the underlying data model?

Thanks
Amber
 
G

Guest

I guess dtEmp is a DataTable...all you have to do is sort the dataTable before you set it as the datasource
 
W

William Ryan eMVP

Create a DataView based on the table...

Dim myView as DataView = dtEmp.DefaultView
Then you can sort on any field you want. From there, just substitute
..DataSource = dtEmp with .DataSource = myView

HTH,

Bill

www.devbuzz.com
www.knowdotnet.com

amber said:
Hello,
I use the following code to populate a combobox:

Me.cbxPeerReview.DropDownStyle = ComboBoxStyle.DropDownList
With cbxPeerReview
.DataSource = dtEmp
.DisplayMember = "STR_NAME_F"
.ValueMember = "ID_USER"
End With
Me.cbxPeerReview.DroppedDown = True
Me.cbxPeerReview.SelectedIndex = -1

I want to sort to combobox by the "STR_NAME_F", but if I add the .sorted =
true line, I get the following error:
An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll
Additional information: Cannot sort a ComboBox that has a DataSource set.
Sort the data using the underlying data model.
 

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