DataTable dt=new DataTable();
新增行:
DataRow addDR= mydatatable.NewRow();
addDR["ID"] = "20";
addDR["Name"] = "Tom";
addDR["Grade"] = "计算机一班";
dt.Rows.Add(addDR);
dt.AcceptChanges();
修改行:
DataColumn[] dtKey = new DataColumn[1];
dtKey[0] = dtFieldUse.Columns["Id"];
dtFieldUse.PrimaryKey = dtKey; //设置tatatable主键
DataRow modDr = dtFieldUse.Rows.Find(colId);
DataRow[] dr = dtFS.Select("ColumnId=" + columnId + " AND FieldId=" + colId);//查询datatable
modDr["FieldUseId"] = dr[0]["Id"];
modDr["Sort"] = dr[0]["Sort"];
modDr["IsShowList"] = dr[0]["IsShowList"]; m
odDr["IsRequired"] = dr[0]["IsRequired"];
modDr["FailureMsg"] = dr[0]["FailureMsg"];
modDr["IsSearch"] = dr[0]["IsSearch"];
dtFieldUse.AcceptChanges();
删除行:
DataColumn[] myPrimaryKey = newDataColumn[1];
myPrimaryKey[0] = mydatatable.Columns["ID"];
dt.PrimaryKey = myPrimaryKey;
DataRow myRemoveRow = dt.Rows.Find("20");
dt.Delete();
dt.AcceptChanges();
查询datatable:
DataRow[] dr = dt.Select("ColumnId=" + columnId + " AND FieldId=" + colId);//查询datatable
编历datatable:
foreach(DataRow dr in dt.Rows)
{
Console.WriteLine("ID="+dr["ID"]);
}