如何在combox中对数据进行排序 [英] how to sort data in combox
问题描述
我想在combox中对数据进行排序
combox中的数据是从datatable中的列中获取的
我使用以下代码进行获取数据到combox
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
comboBox1.DataSource = WorkersMgr.GetALLWorkers();
comboBox1.DisplayMember = "Full_Name";
comboBox1.ValueMember = "Worker_ID";
注意:GetAllWorkers()从工人表中返回所有工人
note : GetAllWorkers() returns all workers from workers table
推荐答案
在<$ c中编写的代码是什么$ c> GetAllWorkers(),您需要修改该代码,以便在从数据库中选择数据时对数据进行排序。
就像你是像这样直接点击选择查询
what was the code written inGetAllWorkers()
, 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
SELECT FULL_NAME,WORKER_ID FROM WORKSRS
那么你需要改变这样的代码
then you need to change your code like this
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.
使用DataView而不是直接使用DataTable的最简单方法:
The easiest way it to use a DataView, instead of the DataTable directly:
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";
从同一数据中设置两个组合,并对其中一个进行排序。
错字:DateTable forDataTable - OriginalGriff [/ edit]
Sets up two combos from the same data, and sorts one of them.
[edit]Typo: "DateTable" for "DataTable" - OriginalGriff[/edit]
创建数据表
将数据库值分配给datatabel
现在排序数据表datatable.select()
将数据表分配给ddl
Create a datatable
assign your database value to datatabel
now sort datatable datatable.select()
assign datatable to ddl
这篇关于如何在combox中对数据进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!