可判断datatable的列是否有重复

时间:2024-03-09 11:40:36

 DataTable dt = new DataTable();       

dt.Columns.Add("id");  

 dt.Columns.Add("value");

DataRow dr = dt.NewRow();

 dr["id"] = "1"; 

dr["value"] = "1";            

dt.Rows.Add(dr);            

dr = dt.NewRow();        

dr["id"] = "1";   

 dr["value"] = "2";

 dt.Rows.Add(dr);

 dr = dt.NewRow();   

dr["id"] = "2";  

dr["value"] = "22";

dt.Rows.Add(dr);      

//将dt中的重复数据过滤掉

DataView myDataView = new DataView(dt); 

 //此处可加任意数据项组合 

string[] strComuns = { "id"};

 dt = myDataView.ToTable(true, strComuns);