对数据表中的数据进行排序,然后将其绑定到gridview [英] sort data in datatable then bind it to gridview
本文介绍了对数据表中的数据进行排序,然后将其绑定到gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在数据表中有数据
首先,我想将其按A/C排序到我的datatable(Empcode)的第一个col,然后将其与gridview绑定
我应该怎么做??
基本上我是从文本文件中获取数据并将其放入数据表,然后将其绑定到gridview
帮助!!!
I have Data in Datatable
First i want to sort it a/c to my first col of my datatable(Empcode) then i want to bind it with gridview
How should i do it???
Basically I m taking data from text file to put it into datatable then bind it to gridview
Help !!!
推荐答案
使用DataView
对数据进行排序.
UseDataView
to sort your data.
DataView dv = new DataView();
dv = dt.DefaultView; //cosidering dt as your datatable which contains data
dv.Sort = "SortExpression";
在这里,SortExpression是您的排序列.
您也可以在排序表达式中提供排序方向.
像这样的东西:
Here, SortExpression is your sort column(s).
You may also provide sort direction with the sort expression.
Something like this:
dv.Sort = "EmpCode" + " ASC"; //for ascending
dv.Sort = "EmpCode" + " DESC"; //for descending
然后将GridView
绑定到DataView
希望这会有所帮助!
Then bind the GridView
to the DataView
Hope this helps!
如果您的情况如此,在这种情况下,我将采用LINQ.
使用
dtobject.AsEnumerable().OrderBy(o => o["colname"].ToString());
像这样的东西...:rose:
If I am in your case, I would employ LINQ in this scenario.
Use
dtobject.AsEnumerable().OrderBy(o => o["colname"].ToString());
something like this ... :rose:
这篇关于对数据表中的数据进行排序,然后将其绑定到gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文