Click here to Skip to main content
15,915,319 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to sort data in combox
data in combox is fetched from column in datatable

I used the following code for fetch data to combox

C#
comboBox1.DataSource = WorkersMgr.GetALLWorkers();
            comboBox1.DisplayMember = "Full_Name";
            comboBox1.ValueMember = "Worker_ID";


note : GetAllWorkers() returns all workers from workers table
Posted

what was the code written in GetAllWorkers(), you need to modify that code to sort your data while selecting it from database.

like if you are directly fire select query like this

SQL
SELECT FULL_NAME,WORKER_ID FROM WORKSRS


then you need to change your code like this

SQL
SELECT FULL_NAME,WORKER_ID FROM WORKRS ORDER BY FULL_NAME ASC  -- if you need to sort data A to Z

OR

SELECT FULL_NAME,WORKER_ID FROM WORKRS ORDER BY FULL_NAME DESC  -- if you need to sort data Z to A



like this you can sort your data first then you need to bind it to your combobox, you need to write your own query based on your database structure.
 
Share this answer
 
The easiest way it to use a DataView, instead of the DataTable directly:
C#
DataTable dt = new DataTable();
dt.Columns.Add("MyColumn");
dt.Rows.Add("123456");
dt.Rows.Add("923456");
dt.Rows.Add("523456");
dt.Rows.Add("223456");
dt.Rows.Add("023456");
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "MyColumn";
DataView dv = new DataView(dt);
dv.Sort = "MyColumn";
comboBox2.DataSource = dv;
comboBox2.DisplayMember = "MyColumn";
Sets up two combos from the same data, and sorts one of them.

[edit]Typo: "DateTable" for "DataTable" - OriginalGriff[/edit]
 
Share this answer
 
v2
Create a datatable

assign your database value to datatabel

now sort datatable datatable.select()

assign datatable to ddl
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900