把数据增加到datatable的方法
向DataTable中添加一行数据
<%@Import namespace="System.Data" %>
<script language="C#" runat="server">
protected void Page_Load(object o, EventArgs e) {
datagrid.DataSource = GetData();
DataBind();
}DataTable GetData() {
DataTable data = new DataTable();
data.Columns.Add(new DataColumn("TheID", typeof(Int32)));
data.Columns.Add(new DataColumn("Name", typeof(string)));
data.Columns.Add(new DataColumn("TimeZone", typeof(string)));DataRow dr;
dr = data.NewRow();
dr[0] = 1; dr[1] = "Washington"; dr[2] = "Pacific";
data.Rows.Add(dr);
dr = data.NewRow();
dr[0] = 2; dr[1] = "Utah"; dr[2] = "Mountain";
data.Rows.Add(dr);
dr = data.NewRow();
dr[0] = 3; dr[1] = "Wisconsin"; dr[2] = "Central";
data.Rows.Add(dr);return data;
}
</script>
<form runat="server">
<asp教程:DataGrid runat="server" id="datagrid" />
</form>来自网络
手动插入一行数据
DataSet ds = tTalent.GetAllInfo();
DataRow dr = ds.Tables[0].NewRow();
dr["id"] = 0;
dr["aboutType"] = "常见问题";
dr["contents"] = "";
ds.Tables[0].Rows.Add(dr);
this.dlTalent.DataSource = ds.Tables[0].DefaultView;
this.dlTalent.DataBind();插入一列数据
DataTable dt = DBHelper.GetDataTable(sql);
dt.Columns.Add("num", Type.GetType("System.Int32"));
for (int i = 0; i < dt.Rows.Count; i++)
{
dt.Rows[i]["num"] = i + 1;
}DataTable tblDatas = newDataTable("Datas");
tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
tblDatas.Columns[0].AutoIncrement = true;
tblDatas.Columns[0].AutoIncrementSeed = 1;
tblDatas.Columns[0].AutoIncrementStep = 1;tblDatas.Columns.Add("Product",Type.GetType("System.String"));
tblDatas.Columns.Add("Version",Type.GetType("System.String"));
tblDatas.Columns.Add("Description",Type.GetType("System.String"));tblDatas.Rows.Add(newobject[]{null,"a","b","c"});
tblDatas.Rows.Add(newobject[] { null, "a", "b", "c" });
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });