1、创建DataTable对象

DataTable dt = new DataTable("Table_AX");

2、为DataTable创建列

//方式一(我觉得这种好)
dt.Columns.Add("column0", System.Type.GetType("System.String"));
//方式二
DataColumn dc = new DataColumn("column0", typeof(string));
DataColumn dc = new DataColumn("column1", System.Type.GetType("System.Boolean"));
dt.Columns.Add(dc);

3、添加数据

DataRow dr = dt.NewRow();
dr["column0"] = "AX";
dr["column1"] = true;
dt.Rows.Add(dr);

4、移除数据

dt.Columns.Remove("column0");

4、判断是否有数据

//判断是否有数据
if (dt.Rows.Count > )
{
//有
}

5、查询

DataRow[] drs = dt.Select("column1 is null");
DataRow[] drss = dt.Select("column0 = 'AX'");
//返回第一条
DataRow dr = dt.Select("column1 is null").First();
//多条件用and连接
DataRow[] rows = dt.Select("column0 = 'AX' and column1 == true").First();

6、复制

//复制一个DataTable(有数据)
DataTable dtNew = dt.Copy();
//复制一个空的DataTable(无数据)
DataTable dtOnlyScheme = dt.Clone();
//将 DataRow 复制到 DataTable 中,保留任何属性设置以及初始值和当前值
DataRow[] rows = dt.Select("column0 = 'AX' and column1 == true").First();
foreach (DataRow row in rows)
{
dtNew.ImportRow(row);//复制行数据
}

7、DataRow赋值

DataRow drOperate = dt.Rows[];
//方式一
drOperate["column0"] = "AXzhz";
drOperate["column1"] = false;
//方式二
drOperate[] = "AXzhz";
drOperate[] = false;
//方式三
dt.Rows[]["column0"] = "AXzhz";
dt.Rows[]["column1"] = false;
//方式四
dt.Rows[][] = "AXzhz";
dt.Rows[][] = false;

8、把DataRow数据存到DataTable中

dtOnlyScheme.Rows.Add(dt.Rows[].ItemArray);

9、转成字符串

System.IO.StringWriter sw = new System.IO.StringWriter();
System.Xml.XmlTextWriter xw = new System.Xml.XmlTextWriter(sw);
dt.WriteXml(xw);//此处DataTable必须有名字
string s = sw.ToString();

10、筛选数据(详见DefaultView文章)

dt.DefaultView.RowFilter = "column1 <> true";
dt.DefaultView.RowFilter = "column1 = true";

11、排序

dt.DefaultView.Sort = "column0 ,column1 ASC";
dt = dt.DefaultView.ToTable();

12、判断一个字符串是否为DataTable的列名

bool flag = dt.Columns.Contains("AX");

13、取值

string str =dt.Rows[第几行]["字段名"].ToString();
string str =dt.Rows[第几行][第几列].ToString(); //for循环
for (int i = ; i < dt.Rows.Count; i++)
{
string str = dt.Rows[i]["字段名"].ToString();
string str = dt.Rows[i][第几列].ToString();
}
//foreach循环
foreach (DataRow dr in dt.Rows)
{
string str = dr["字段名"].ToString();
}
//创建DataTable对象
DataTable dt = new DataTable("Table_AX");
//为DataTable创建列
//方式一(我觉得这种好)
dt.Columns.Add("column0", System.Type.GetType("System.String"));
//方式二
DataColumn dc = new DataColumn("column1", System.Type.GetType("System.Boolean"));
dt.Columns.Add(dc);
//添加数据
DataRow dr = dt.NewRow();
dr["column0"] = "AX";
dr["column1"] = true;
dt.Rows.Add(dr); //判断是否有数据
if (dt.Rows.Count > )
{
//有
} //查询
DataRow[] drs = dt.Select("column1 is null");
DataRow[] drss = dt.Select("column0 = 'AX'"); //复制一个DataTable(有数据)
DataTable dtNew = dt.Copy();
//复制一个空的DataTable(无数据)
DataTable dtOnlyScheme = dt.Clone(); //给DataRow赋值的三种方式
DataRow drOperate = dt.Rows[];
//方式一
drOperate["column0"] = "AXzhz";
drOperate["column1"] = false;
//方式二
drOperate[] = "AXzhz";
drOperate[] = false;
//方式三
dt.Rows[]["column0"] = "AXzhz";
dt.Rows[]["column1"] = false;
//方式四
dt.Rows[][] = "AXzhz";
dt.Rows[][] = false; //把DataRow数据存到DataTable中
dtOnlyScheme.Rows.Add(dt.Rows[].ItemArray); //转成字符串
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Xml.XmlTextWriter xw = new System.Xml.XmlTextWriter(sw);
dt.WriteXml(xw);//此处DataTable必须有名字
string s = sw.ToString(); //筛选数据(详见DefaultView文章)
dt.DefaultView.RowFilter = "column1 <> true";
dt.DefaultView.RowFilter = "column1 = true"; //排序
dt.DefaultView.Sort = "column0 ,column1 ASC";
dt = dt.DefaultView.ToTable(); //绑定的其实是DefaultView
//gvTestDataTable.DataSource = dt;
//gvTestDataTable.DataBind(); //判断一个字符串是否为DataTable的列名
bool flag = dt.Columns.Contains("AX");

整合代码

05-11 13:06