构建表DataTable PCDT = new DataTable();数据类型不设置也可以DataColumn dc0 = new DataColumn("PCID",typeof(int));
PCDT.Columns.Add(dc0);
DataColumn dc1 = new DataColumn("PCName");
PCDT.Columns.Add(dc1);
//设置主键
DataColumn[] dc = new DataColumn[1];
dc[0] = PCDT.Columns[0];
PCDT.PrimaryKey = dc;
2.给DataTable添加数据
DataRow dr;
dr = dtRefresh.NewRow();
dr["PCName"] = "zhxhdean";
PCDT.Rows.Add(dr);
3.查询DataTable
1:绑定到数据控件
DataView dv = PCDT.DefaultView;
//设置过滤器即T-Sql中where条件
dv.RowFilter = "id>=2";
//绑定到数据控件
this.GridView1.DataSource = dv;
this.GridView1.DataBind();
2.普通取值
DataRow[] dr = PCDT.Select("PInputPerson='zhxhdean'");
//下面对dr进行操作....
4.DataTable合并
//Dt1 , Dt2
//将Dt2接在Dt1后可采用此方法
dt1.Merge(dt2);
//复制表结构使用Clone()
dt1 = dt2.Clone();
//复制表结构及内容用Copy()
dt1 = dt2.Copy();
5.DataTable排序
PCDT.DefaultView.sort="lastName";
PCDT.DefaultView.sort="lastName,FirstName DESC";