DataTable DateRow

来源:互联网 发布:八大处 知乎 编辑:程序博客网 时间:2024/05/21 10:56
 

在DataSet中创建DataTable:


DataSet ds = new DataSet("MyDataSet");
DataTable table = new DataTable("MyDataTable");
ds.Tables.Add(table);

使用语法糖:


DataSet ds = new DataSet("MyDataSet");
DataTable table = ds.Tables.Add("MyDataTable");

在DataTable中创建DataColumn

直接使用语法糖:


DataColumn col1 = table.Columns.Add("ID");
DataColumn col2 = table.Columns.Add("Name");

可以为DataColumn指定数据类型:


DataColumn col1 = table.Columns.Add("ID",typeof(int));
DataColumn col2 = table.Columns.Add("Name",typeof(string));

增加DataRow:


DataRow row = table.NewRow();
row["ID"] = 1;
row["Name"] = "LMF";
table.Rows.Add(row);

使用语法糖:


table.Rows.Add(1,"lmf");

修改现有行数据,在拥有一个row对象之后,可以使用对象的Item属性来设定一列数据的值,这个属性是可读写的,代码:


if(row!=null)
{
    row["Name"] = "ming";
}

删除DataRow:


row.Delete();

移除DataRow:


1、table.Rows.Remove(row);
//根据行的索引移除
2、table.Rows.RemoveAt(table.Rows.IndexOf(row));

 还有DataSet和DataTable类都具有一个Clear(),使用这个方法后会删除全部的DataRow对象,同时保留其结构:


table.Clear();