我们如何在C#.net中对数据表进行排序? [英] How can we sort datatables in C#.net?

查看:89
本文介绍了我们如何在C#.net中对数据表进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何在C#.net中对数据表进行排序?

How can we sort datatables in C#.net?

推荐答案

您可以使用Sort属性对数据表进行排序

you can sort datatables by using Sort property

datatables1.DefaultView.Sort = "column1"; 


您不能直接对DataTable进行排序,
用于对DataTable进行排序,将其转换为GrideView

或将其转换为DataView
这是可能对您有帮助的示例代码.
You can''t sort DataTable directly,
for to sort DataTable convert it to GrideView

or convert it into DataView
here is the sample code that may helps you.
DataTable dt = new DataTable();
           DataRow dr;

           // Define the columns of the table.
           dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
           dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
           dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));

           // Populate the table with sample values.
           for (int i = 0; i < 5; i++)
           {
               dr = dt.NewRow();

               dr[0] = i;
               dr[1] = "Item " + i.ToString();
               dr[2] = 5.23 * ((5-i) + 1);

               dt.Rows.Add(dr);
           }
           DataView dv = new DataView(dt);
           dv.Sort = "CurrencyValue";
           dt = dv.ToTable();


DataView的sort属性采用字符串类型,该类型包含列名,后跟"ASC"(升序)或"DESC"(降序).默认情况下,列按升序排序.多个列可以用逗号分隔.


sort property of DataView takes string type, that contains the column name followed by "ASC" (ascending) or "DESC" (descending). Columns are sorted ascending by default. Multiple columns can be separated by commas.

dv.Sort = "CurrencyValue DESC";


在以下URl中进行浏览

http://forums.asp.net/t/1396828.aspx/1 [ ^ ]

http://weblogs.asp.net/vikram/存档/2008/02/13/sorting-dataview-on-multiple-columns.aspx [
Go throuth this URl below

http://forums.asp.net/t/1396828.aspx/1[^]

http://weblogs.asp.net/vikram/archive/2008/02/13/sorting-dataview-on-multiple-columns.aspx[^]


这篇关于我们如何在C#.net中对数据表进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆