hi every1..
i want to sort my combo box bt the pblem is that that i have bind it with a database.
in the properties window i have set sorted = true bt it says

ComboBox that has a DataSource set cannot be sorted. Sort the data using the underlying data model.

Can any1 help me... thanks

Dani AI

Generated

For : a bound ComboBox will not reorder itself — the data must be sorted before it is assigned as the DataSource. As and noted, sorting at the source (SQL) is the simplest approach for large sets. When sorting client-side, a DataView, BindingSource or LINQ can be used to produce an ordered list and then bind that to the ComboBox.

Example: sort via the DataTable.DefaultView and bind that view (VB.NET)

Dim dt As DataTable = GetDataFromDatabase()
dt.DefaultView.Sort = "Name ASC"
ComboBox1.DisplayMember = "Name"
ComboBox1.ValueMember = "ID"
ComboBox1.DataSource = dt.DefaultView

Alternative: use a BindingSource.Sort or LINQ-to-DataSet

Dim bs As New BindingSource()
bs.DataSource = dt
bs.Sort = "Name ASC"
ComboBox1.DisplayMember = "Name"
ComboBox1.ValueMember = "ID"
ComboBox1.DataSource = bs
Dim sorted = dt.AsEnumerable().
    OrderBy(Function(r) r.Field(Of String)("Name")).
    CopyToDataTable()
ComboBox1.DataSource = sorted

Troubleshooting notes and gotchas:

  • CopyToDataTable() throws on an empty sequence; guard with a row check or use dt.Clone() to create an empty schema copy.
  • Sort expressions use column names; column names containing spaces need brackets (e.g., [Full Name]).
  • Sorting honors the column data type (numeric vs string) and culture rules; for case-insensitive string ordering, project a normalized value in the sort key.
  • BindingSource.Sort requires an underlying list that supports sorting (DataView works).
  • For large result sets prefer SQL-side sorting for performance.

For API details see the .NET docs for DataView.Sort (DataView.Sort).

Recommended Answers

All 5 Replies

i tried to binding data into combo box and set Sorted = true and it works fine...

Hi mine it doesn't work any another way

u can sort it in database itself. u can use order by in ur query i think.

u can sort it in database itself. u can use order by in ur query i think.

yes, this an another solution.
use order by ColumName ASC (to sort ascending)
use order by ColumName DESC (to sort Descending)

Yes if u sort it in database itself then while loading combo itself it get sorted..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.