45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:使用asp.net的Datatable的步骤

使用asp.net的Datatable的步骤

2016-08-26 23:04:13 来源:www.45fan.com 【

使用asp.net的Datatable的步骤

有时需要在内存中创建一个临时表,用以实现特殊的显示和查询要求,而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();
}
}


本文地址:http://www.45fan.com/a/question/68136.html
Tags: 创建 ASP.NET DATATABLE
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部