static void Main(
string[] args)
{
DataTable dt = InitialData();
//查找 DataTable 中 A列 值 大于中 A_3 的列 DataRow[] drs = dt.Select(
"A > 'A_3'",
"A desc");
Console.WriteLine(
"A\tB\tC\tD");
//遍历数据 foreach (DataRow row
in drs)
{
string a = row[
"A"]
as string;
string b = row[
"B"]
as string;
string c = row[
"C"]
as string;
string d = row[
"D"]
as string;
Console.WriteLine(a +
"\t" + b +
"\t" + c +
"\t" + d);
}
Console.ReadKey();
}
/// <summary>/// 初始化测试数据/// </summary>public static DataTable InitialData()
{
//创建一个名为:TestTable的数据表 DataTable dt =
new DataTable(
"TestTable");
//为表创建 4列,分别为: A 、 B 、 C 、 D dt.Columns.Add(
new DataColumn(
"A"));
dt.Columns.Add(
new DataColumn(
"B"));
dt.Columns.Add(
new DataColumn(
"C"));
dt.Columns.Add(
new DataColumn(
"D"));
for (
int i = 1; i <= 10; i++)
{
//添加新的数据行,并添加数据 DataRow dr = dt.NewRow();
dr[
"A"] =
"A_" + i;
dr[
"B"] =
"B_" + i;
dr[
"C"] =
"C_" + i;
dr[
"D"] =
"D_" + i;
//将此行添加到DataTable中 dt.Rows.Add(dr);
}
return dt;
}