您可以使用 LINQ 对 DataTable 进行排序。 LINQ 操作 (dt2) 的结果是 IEnumerable,但您仍然可以使用它来绑定您的网格。
var dt = new DataTable();
dt.Columns.Add("Key", typeof(string));
dt.Columns.Add("Position", typeof(int));
dt.Rows.Add("Key1Value", 2);
dt.Rows.Add("Key2Value", 3);
dt.Rows.Add("Key3Value", 4);
dt.Rows.Add("Key4Value", 5);
dt.Rows.Add("Key5Value", 0);
dt.Rows.Add("Key6Value", 1);
var dt2 = dt.AsEnumerable().OrderBy(x => x.Field<int>("Position"));
foreach (DataRow dr in dt2)
{
Console.WriteLine("{0} {1}", dr["Key"], dr["Position"]);
}
我编辑了我的代码,使其符合您的排序顺序。我添加了一个名为 Position 的列,因此我可以随意对其进行排序。如果您想要不同的订单,只需更改 Position 的值即可。
如果不想多加一列,可以在 OrderBy 方法中使用 switch 语句。
var dt = new DataTable();
dt.Columns.Add("Key", typeof(string));
dt.Rows.Add("Key1Value");
dt.Rows.Add("Key2Value");
dt.Rows.Add("Key3Value");
dt.Rows.Add("Key4Value");
dt.Rows.Add("Key5Value");
dt.Rows.Add("Key6Value");
var dt2 = dt.AsEnumerable().OrderBy(x =>
{
switch (x.Field<string>("Key"))
{
case "Key5Value": return 0;
case "Key6Value": return 1;
case "Key1Value": return 2;
case "Key2Value": return 3;
case "Key3Value": return 4;
case "Key4Value": return 5;
default : return -1;
}
}
);
foreach (DataRow dr in dt2)
{
Console.WriteLine(dr["Key"]);
}
基本上,OrderBy() 方法接受定义排序顺序的匿名方法。由于“Key5Value”返回 0,因此将其放在“Key6Value”之前(返回 1)。如果要将其从最大值到最小值排序,请使用 OrderByDescending() 方法。
我还编辑了您的代码。 getPosition 方法类似于我的 LINQ 代码中的 OrderBy() 方法。
using System;
using System.Data;
using System.Linq;
class Program
{
static void Main()
{
var dt = new DataTable();
dt.Columns.Add("Key", typeof(string));
dt.Rows.Add("Key1Value");
dt.Rows.Add("Key2Value");
dt.Rows.Add("Key3Value");
dt.Rows.Add("Key4Value");
dt.Rows.Add("Key5Value");
dt.Rows.Add("Key6Value");
var dt2 = dt.Clone();
foreach (DataRow dr in dt.Rows)
{
string key = dr["Key"].ToString();
var dRow = dt2.NewRow();
dRow.ItemArray = dr.ItemArray;
dt2.Rows.InsertAt(dRow, getPosition(key));
}
foreach (DataRow dr in dt2.Rows)
{
Console.WriteLine(dr["Key"]);
}
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
static int getPosition(string key)
{
switch (key)
{
case "Key5Value":
return 0;
case "Key6Value":
return 1;
case "Key1Value":
return 2;
case "Key2Value":
return 3;
case "Key3Value":
return 4;
case "Key4Value":
return 5;
default:
return -1;
}
}
}
版本 2:
我添加了一个字典,其中包含键值(例如 Key1Value)和一对值。对值是新的键值(例如键 1 值)和位置。我使用字典的原因是替换 switch-case 表达式。如果需要,您可以添加额外的价值。只需向元组添加更多类型。
Tuple<string, int, int, bool> // holds a sequence of a string, two integers and a boolean.
这是完整的代码。
using System;
using System.Data;
using System.Linq;
using System.Collections.Generic;
class Program
{
static void Main()
{
// create table
var dt = new DataTable();
dt.Columns.Add("Key", typeof(string));
dt.Columns.Add("Val1", typeof(int));
dt.Columns.Add("Val2", typeof(int));
dt.Columns.Add("Val3", typeof(int));
dt.Columns.Add("Position", typeof(int));
// populate table
dt.Rows.Add("Key1Value", 100, 200, 300);
dt.Rows.Add("Key2Value", 100, 200, 300);
dt.Rows.Add("Key3Value", 100, 200, 300);
dt.Rows.Add("Key4Value", 100, 200, 300);
dt.Rows.Add("Key5Value", 100, 200, 300);
dt.Rows.Add("Key6Value", 100, 200, 300);
// initialize dictionary
var dict = new Dictionary<string, Tuple<string, int>>() {
{ "Key1Value", new Tuple<string, int>("Key 1 Value", 2) }, // Key1Value's new value is Key 1 Value, position is 2
{ "Key2Value", new Tuple<string, int>("Key 2 Value", 3) },
{ "Key3Value", new Tuple<string, int>("Key 3 Value", 4) },
{ "Key4Value", new Tuple<string, int>("Key 4 Value", 5) },
{ "Key5Value", new Tuple<string, int>("Key 5 Value", 0) },
{ "Key6Value", new Tuple<string, int>("Key 6 Value", 1) }
};
// set position and new key value
dt.AsEnumerable()
.ToList()
.ForEach(x => {
x["Position"] = dict[x.Field<string>("Key")].Item2;
x["Key"] = dict[x.Field<string>("Key")].Item1; // must be the last because the dictionary key is "KeyXValue", not "Key X Value"
});
// sort by Position
var dt2 = dt.AsEnumerable().OrderBy(x => x.Field<int>("Position"));
foreach (DataRow dr in dt2)
{
Console.WriteLine("{0}, {1}, {2}, {3}, {4}", dr["Key"], dr["Val1"], dr["Val2"], dr["Val3"], dr["Position"]);
}
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}