简介:
首先可建立一个表,对表进行填充若干条数据,代码如下:
//创建Table1 DataTable dt = new DataTable(); //对Table1添加列名,并设置列值类型 DataTable dt1 = new DataTable();//创建Table1 dt1.Columns.Add("ID", typeof(string)); dt1.Columns.Add("NAME", typeof(string)); dt1.Columns.Add("AGE", typeof(int)); dt1.Columns.Add("SEX", typeof(string)); //对Table1进行设置KEY //dt1.PrimaryKey = new DataColumn[] { dt1.Columns["ID"] }; //对Tabel添加数值 for (int i = 0; i < 5; i++) { DataRow dr = dt1.NewRow(); dr["ID"] = "00" + i.ToString(); dr["NAME"] = "00-" + i.ToString(); dr["AGE"] = 15 + i; dr["SEX"] = "M"; dt1.Rows.Add(dr); } DataTable dtcopy = dt1.Copy();
1.实现对Table的排序,可以实现按多个栏位进行排序
//排序 dtcopy.DefaultView.Sort = "ID DESC,AGE ASC"; dtcopy = dtcopy.DefaultView.ToTable();
2.对Table进行筛选数据
//筛选 dtcopy.DefaultView.RowFilter = "AGE > 20"; dtcopy = dtcopy.DefaultView.ToTable();
3.建立一行列值都为空的数据
//添加一行空行 注:各列必须没有Key值限制 dtcopy.DefaultView.AddNew(); dtcopy = dtcopy.DefaultView.ToTable();