因此,我正在尝试对一些数据进行排序并打印,但是cid
似乎并没有达到我期望的效果。鉴于此数据:
DataTable.DefaultView.Sort
然后按“ Col2”排序,我得到:
Col1 Col2 Col3
2802 32 0
2802 80 3
2802 130 3
2802 40 0
2802 40 0
2802 35 0
我编写了一个简单的排名库来执行我期望的操作(2802 130 3
2802 32 0
2802 35 0
2802 40 0
2802 40 0
2802 80 3
),它位于此代码段的下方,但是它仅按1列进行排序,因此我更喜欢使用DataTableRanker.Framework.Util
等:
.Sort = Col1, Col2, Col3,
这里是using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DataTableRanker.Framework.Util;
namespace Test
{
class Program
{
static void Main(string[] args)
{
DataTable testTable = new DataTable();
testTable.Columns.Add("FacilityID");
testTable.Columns.Add("FromDimensions");
testTable.Columns.Add("FromAttributeName");
DataRow dr1 = testTable.NewRow();
dr1["FacilityID"] = "2802";
dr1["FromDimensions"] = 4*8;
dr1["FromAttributeName"] = 0;
testTable.Rows.Add(dr1);
DataRow dr2 = testTable.NewRow();
dr2["FacilityID"] = "2802";
dr2["FromDimensions"] = 8*10;
dr2["FromAttributeName"] = 3;
testTable.Rows.Add(dr2);
DataRow dr3 = testTable.NewRow();
dr3["FacilityID"] = "2802";
dr3["FromDimensions"] = 10*13;
dr3["FromAttributeName"] = 3;
testTable.Rows.Add(dr3);
DataRow dr4 = testTable.NewRow();
dr4["FacilityID"] = "2802";
dr4["FromDimensions"] = 4*10;
dr4["FromAttributeName"] = 0;
testTable.Rows.Add(dr4);
DataRow dr5 = testTable.NewRow();
dr5["FacilityID"] = "2802";
dr5["FromDimensions"] = 4 * 10;
dr5["FromAttributeName"] = 0;
testTable.Rows.Add(dr5);
DataRow dr6 = testTable.NewRow();
dr6["FacilityID"] = "2802";
dr6["FromDimensions"] = 5*7;
dr6["FromAttributeName"] = 0;
testTable.Rows.Add(dr6);
DataTable copyTestTable;
copyTestTable = testTable.Copy();
copyTestTable.Columns.Add("Ranking");
Console.Write("-- Not Sorted --");
Console.WriteLine();
foreach (DataRowView dr in testTable.DefaultView)
{
for (int i = 0; i < testTable.DefaultView.Table.Columns.Count; i++)
Console.Write(dr[i] + "\t");
Console.WriteLine();
}
testTable.DefaultView.Sort = "FromDimensions";
Console.Write("-- Sorted --");
Console.WriteLine();
foreach (DataRowView dr in testTable.DefaultView)
{
for (int i = 0; i < testTable.DefaultView.Table.Columns.Count; i++)
Console.Write(dr[i] + "\t");
Console.WriteLine();
}
Console.Write("-- Ranked with System.Data --");
Console.WriteLine();
testTable.Columns.Add(new DataColumn("Rank", typeof(int)));
int count = 1;
foreach (DataRowView dr in testTable.DefaultView)
{
dr["Rank"] = count++;
}
foreach (DataRowView dr in testTable.DefaultView)
{
for (int i = 0; i < testTable.DefaultView.Table.Columns.Count; i++)
Console.Write(dr[i] + "\t");
Console.WriteLine();
}
Console.Write("-- Ranked with DataTableRanker --");
Console.WriteLine();
DataTableSort u1 = new DataTableSort();
u1.RankDataTable(ref copyTestTable, "FromDimensions", "Ranking");
copyTestTable.DefaultView.Sort = "Ranking";
foreach (DataRowView dr in copyTestTable.DefaultView)
{
for (int i = 0; i < copyTestTable.DefaultView.Table.Columns.Count; i++)
Console.Write(dr[i] + "\t");
Console.WriteLine();
}
}
}
}
:
DataTableRanker.Framework.Util
非常感谢您的帮助或解释!
答案 0 :(得分:1)
如注释中所建议,排序顺序基于字符串。 1在3之前,因此130在31之前出现。这是添加没有其他选项的新列时的默认行为
在DataTable class documentation上的示例中,首先看一下创建一列并设置其数据类型。
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
table.Columns.Add(column);
更新: 感谢ESG在评论中指出DataColumnCollection.Add有一个重载,该重载也将采用一种类型。