如何在datatable中对小数位数进行排序? [英] how to sort decimal place number in datatable?

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

问题描述

如何在Datatable或Dataview中对小数位数进行排序。







i有如下数据





13.5

13.9

13.1

13.2



i希望在Datatable或Dataview中的小数位后排序数据。请给我解决方案。

How to Sort Decimal Place number in Datatable Or Dataview.



i have data like below


13.5
13.9
13.1
13.2

i want to Sort data after decimal places in Datatable or Dataview. Please give me solution for that.

推荐答案

见这里: http://stackoverflow.com/questions/89987/how-to-naturally-sort-a-dataview-with-something-like-icomparable [ ^ ]



或者更好的是,当你想要数字时,不要将字符串放入数据中:改为使用数字字段。它们正确和正常排序。
See here: http://stackoverflow.com/questions/89987/how-to-naturally-sort-a-dataview-with-something-like-icomparable[^]

Or better, don't put strings into your data when you want numbers: use numeric fields instead. They sort correctly and normally.


1。在上表中创建一个数据克隆。

2.根据需要在clone列中指定数据类型。例如。十进制列的System.Decimal。

3.将原始表中的每一行导入克隆表。

4.提交克隆表中的更改。

5.在克隆表上创建一个DataView。

6.指定DataView的排序列和排序顺序。

7.将DataView绑定到GridView。







1. Create a clone of the data in the above table that you have.
2. Specify the Data Type in clone table, for the sort column as needed. Eg. System.Decimal for decimal column.
3. Import each row from original table to clone table.
4. Commit the changes in clone table.
5. Create a DataView on clone table.
6. Specify the sort column and sort order for the DataView.
7. Bind DataView to GridView.



DataTable dtMarks1 = dtMarks.Clone();
            dtMarks1.Columns["Total"].DataType = Type.GetType("System.Decimal");

            foreach (DataRow dr in dtMarks.Rows)
            {
                dtMarks1.ImportRow(dr);
            }
            dtMarks1.AcceptChanges();


            DataView dv = dtMarks1.DefaultView;
            dv.Sort = "Total DESC";

            GridView1.DataSource = dv;
            GridView1.DataBind();


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

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