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;
}