DataTable 过滤、求和、去重
//多个多个重复去除
//去掉factoryCode,recWeek,materialNo,quantity,recDate,uploadDate相同的重复数据
DataView dv = new DataView(dt); //
dt = dv.ToTable(true, "factoryCode,recWeek,materialNo,quantity,recDate,uploadDate");
//求和
dt.Compute("sum(列名)", "");
//防止页面假死
Application.DoEvents();
//当前dt or ds 根据某列去重
DataView dv = ds.Tables[0].AsDataView();
DataTable DT=dv.ToTable(true, "列名");
DataTable table = new DataTable();
//修改
private void Update()
{
//select里面的写法仿照sql where里面的格式。
DataRow dataRow = table.Select("Name like '张%'")[0];
dataRow["Age"] = 20;
}
//删除
private void Delete()
{
DataRow dataRow = table.Select("Name like '张%'")[0];
table.Rows.Remove(dataRow);
}
//新增
private void Init()
{
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Age", typeof(int));
DataRow dataRow = table.NewRow();
dataRow["Name"] = "张三";
dataRow["Age"] = 18;
table.Rows.Add(dataRow);
dataRow = table.NewRow();
dataRow["Name"] = "李四";
dataRow["Age"] = 28;
table.Rows.Add(dataRow);
dataRow = table.NewRow();
dataRow["Name"] = "王麻子";
dataRow["Age"] = 38;
table.Rows.Add(dataRow);
}
// //判断是否存在重复数据
// DataTable dt = Common.XlsHelper.GetAllExcelDataTable(path, tname);
// //Common.XlsHelper.GetAllExcelDataTable返回一个datatable;
// DataView myDataView = new DataView(dt);
// string[] strComuns = { "mobile" };
// if (myDataView.ToTable(true, strComuns).Rows.Count < dt.Rows.Count)
// {
// //存在重复数据,给出提示,重新输入。
// }
标签:Rows,Name,求和,过滤,dataRow,table,dt,DataTable
From: https://www.cnblogs.com/SDdemon/p/18151053