asp.net 的 Datatable 使用,asp.net c# Datatable 创建 修改 查询 更新 提交 AcceptChanges Select Rows.Add(row)
有时需要在内存中创建一个临时表,用以实现特殊的显示和查询要求,而Datatable 就是经常用到的一个工具,以下是本人的一些测试代码,实现了Datatable的动态创建和查询、修改,由于时间仓促,写得比较乱,不过懂asp.net 和 C#的应该也能看懂,凑合着看吧,希望对大家有所帮助。using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Datatable : System.Web.UI.Page
{
protected DataTable dtYxzt;
protected DataRow row;
protected DataRow[] rows;
private bool IsExist(int id1, int id2)
{
rows = dtYxzt.Select("id1=" + id1 + " and id2=" + id2);
if (rows.Length > 0)
return true ;
else
return false;
}
protected void Page_Load(object sender, EventArgs e)
{
Int16 id1, id2;
dtYxzt = new DataTable("yxzt");
dtYxzt.Columns.Add("id1", typeof(Int16));
dtYxzt.Columns.Add("id2", typeof(Int16));
dtYxzt.Columns.Add("sglx", typeof(string));
dtYxzt.Columns.Add("cb", typeof(string));
dtYxzt.Columns.Add("wsg", typeof(Int32));
dtYxzt.Columns.Add("zzsg", typeof(Int32));
dtYxzt.Columns.Add("sgwc", typeof(Int32));
row = dtYxzt.NewRow();
row["id1"] = 1;
row["id2"] = 1;
row["sglx"] = "新井射孔";
row["cb"] = "采油二厂";
row["wsg"] = 66;
row["zzsg"] = 66;
row["sgwc"] = 66;
dtYxzt.Rows.Add(row);
row = dtYxzt.NewRow();
row["id1"] = 1;
row["id2"] = 2;
row["sglx"] = "新井射孔2";
row["cb"] = "采油二厂2";
row["wsg"] = 66;
row["zzsg"] = 66;
row["sgwc"] = 66;
dtYxzt.Rows.Add(row);
row = dtYxzt.NewRow();
row["id1"] = 1;
row["id2"] = 3;
row["sglx"] = "新井射孔3";
row["cb"] = "采油二厂3";
row["wsg"] = 66;
row["zzsg"] = 66;
row["sgwc"] = 66;
dtYxzt.Rows.Add(row);
dtYxzt.AcceptChanges();
id1 = 1;
id2 = 1;
rows = dtYxzt.Select("id1=" + id1 + " and id2=" + id2);
Response.Write("<br>(1,1)first:" + rows[0]["wsg"].ToString() + "<br>");
id1 = 1;
id2 = 4;
if (!IsExist(id1, id2))
{
row = dtYxzt.NewRow();
row["id1"] = 1;
row["id2"] = 4;
row["sglx"] = "新井射孔4";
row["cb"] = "采油二厂4";
row["wsg"] = 66;
row["zzsg"] = 66;
row["sgwc"] = 66;
dtYxzt.Rows.Add(row);
}
else
{
rows = dtYxzt.Select("id1=" + id1 + " and id2=" + id2);
rows[0]["wsg"] = 77;
}
dtYxzt.AcceptChanges();
rows = dtYxzt.Select("id1=" + id1 + " and id2=" + id2);
Response.Write("<br>new(1,4)" + rows[0]["wsg"].ToString() + "<br>");
id1 = 1;
id2 = 1;
if (!IsExist(id1, id2))
{
row = dtYxzt.NewRow();
row["id1"] = 1;
row["id2"] = 4;
row["sglx"] = "新井射孔4";
row["cb"] = "采油二厂4";
row["wsg"] = 66;
row["zzsg"] = 66;
row["sgwc"] = 66;
dtYxzt.Rows.Add(row);
}
else
{
rows = dtYxzt.Select("id1=" + id1 + " and id2=" + id2);
rows[0]["wsg"] = 77;
}
dtYxzt.AcceptChanges();
rows = dtYxzt.Select("id1=" + id1 + " and id2=" + id2);
Response.Write("<br>更新(1,1)" + rows[0]["wsg"].ToString() + "<br>");
this.DataGrid1.DataSource = dtYxzt;
this.DataBind();
}
}
--------------------编程问答-------------------- 好 顶个 --------------------编程问答-------------------- 好嘛,定顶了 --------------------编程问答-------------------- 好,帮顶
补充:.NET技术 , C#