#region 创建内存表
/// <summary>
/// 创建内存表
/// </summary>
public static void CreateTable(string TableName)
{
DataTable GoodsTable=new DataTable();
GoodsTable.Columns.Add(new DataColumn("Rid",typeof(int)));
GoodsTable.Columns.Add(new DataColumn("GoodsName",typeof(string)));
GoodsTable.Columns.Add(new DataColumn("GoodsNum",typeof(string)));
GoodsTable.Columns.Add(new DataColumn("Attribute",typeof(string)));
GoodsTable.Columns.Add(new DataColumn("Description",typeof(string)));
GoodsTable.Columns.Add(new DataColumn("State",typeof(string)));
GoodsTable.Columns["Rid"].AllowDBNull=false;
GoodsTable.Columns["Rid"].AutoIncrement=true;
GoodsTable.Columns["Rid"].AutoIncrementSeed=0;
GoodsTable.Columns["Rid"].AutoIncrementStep=1;
GoodsTable.Columns["Rid"].Unique=true;
System.Web.HttpContext.Current.Session[TableName]=GoodsTable;
GoodsTable.Dispose();
}
#endregion
#region 修改内存表数据的状态信息
public void UpdateRows(string TableName,string Rid,string State)
{
if(System.Web.HttpContext.Current.Session[TableName]!=null)
{
DataTable Dt=(DataTable)System.Web.HttpContext.Current.Session[TableName];
for(int i=0;i<Dt.Rows.Count;i++)
{
if(Dt.Rows[i][0].ToString()==Rid)
{
Dt.Rows[i][5]=State; //这一处该怎么修改,才可以更新内存表中的数据?
}
}
System.Web.HttpContext.Current.Session[TableName]=Dt;
Dt.Dispose();
}
}
#endregion
用SqlDataAdapter.Update 方法或
DataTable.AcceptChanges方法可更新表数据
Dt.AcceptChange();
加上这个方法
//----------------------------------------------------------------------------------------------
DataTable myTable=DS.Tables["表名"]; --->添加
DataRow myNewRow=myTable.NewRow();
myNewRow["你要添加的列名"]="值";
myNewRow["你要添加的列名"]="值";
myTable.Rows.Add(myNewRow);
dg2.DataSource=myTable.DefaultView;
dg2.DataBind(); //将新添的数据邦定
DataRow[] myNewRow2=myTable.Select("id_jldw=11 AND name_jldw='内容'"); ----->修改
//myNewRow2[0]["要修改的列名"]=值;
myNewRow2[0]["要修改的列名"]=值;
myNewRow2[0]["要修改的列名"]=值;
dg3.DataSource=myTable.DefaultView;
dg3.DataBind();//将新添的数据邦定
myTable.Rows[0].Delete();//删除第一行 ---->删除
dg4.DataSource=myTable.DefaultView;
dg4.DataBind();
//---------------------------------------------------------------------------------------------------
DataTable workTable = new DataTable();
workTable.Columns.Add("LastName", typeof(String));
workTable.ColumnChanged +=
new DataColumnChangeEventHandler(OnColumnChanged);
DataRow workRow = workTable.NewRow();
workRow[0] = "Smith";
workTable.Rows.Add(workRow);
workRow.BeginEdit();
// Causes the ColumnChanged event to write a message and cancel the edit.
workRow[0] = "";
workRow.EndEdit();
// Displays "Smith, New".
Console.WriteLine("{0}, {1}", workRow[0], workRow.RowState);
protected static void OnColumnChanged(
Object sender, DataColumnChangeEventArgs args)
{
if (args.Column.ColumnName == "LastName")
if (args.ProposedValue.ToString() == "")
{
Console.WriteLine("Last Name cannot be blank. Edit canceled.");
args.Row.CancelEdit();
}
}
//--------------------------------------------------------------------------------------------------
for (int i = 0; i < DataTable.Rows.Count; i++)
...{
DataRow dt = DataTable.Rows[i];
dt.BeginEdit();
dt["Column"] = "value";
dt.EndEdit();
}
//-----------------------------------------------------------------------------------------------------
添加:
DataTable cart = new DataTable();
if (Session["shopingcar"] == null)
{
cart.Columns.Add("商品编号", typeof(int));
cart.Columns.Add("商品名称", typeof(string));
cart.Columns.Add("商品单价", typeof(double));
cart.Columns.Add("商品数量", typeof(int));
Session["shopingcar"] = cart;
}
cart = (DataTable)Session["shopingcar"];
DataRow dr = cart.NewRow();
dr["商品编号"] = model.ID;
dr["商品名称"] = model.产品名称;
dr["商品单价"] = double.Parse(model.价格);
dr["商品数量"] = 1;
cart.Rows.Add(dr);
修改:
必须这样:
DataRow row=myTable.Rows[0];
row.BeginEdit();
row.[ "userName "]= "aa ";
row[ "pwd "]= "121 ";
row.EndEdit();
删除某行
代码如下:
删除第二行
myTable.Rows.RemoveAt(1);
//--------------------------------------------------------------------------------------------------------------
有时需要在内存中创建一个临时表,用以实现特殊的显示和查询要求,而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["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();