DataView的使用
DataView对象可以用来筛选、排序、查询、修改(添加、修改、删除)DataTable对象中的数据。
下面是做购物车的程序:
Default.aspx:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Configuration;
usingSystem.Data.SqlClient;
usingSystem.Data;
publicpartialclass_Default:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
Tablet=newTable();
InitTitle(t);
stringstrcnn=ConfigurationManager.ConnectionStrings["cnnstring"].ConnectionString;
using(SqlConnectionsqlCnn=newSqlConnection(strcnn))
{
SqlCommandsqlcmm=newSqlCommand();
sqlcmm.Connection=sqlCnn;
sqlcmm.CommandText="select*frompros";
sqlCnn.Open();
using(SqlDataReaderreader=sqlcmm.ExecuteReader())
{
if(reader!=null)
{
while(reader.Read())
{
FillData(t,reader);
}
}
}
}
this.div1.Controls.Add(t);
}
privatevoidFillData(Tablet,SqlDataReaderreader)
{
TableRowrow=newTableRow();
TableCellcel=newTableCell();
cel.Text=reader["name"].ToString();
row.Cells.Add(cel);
cel=newTableCell();
cel.Text=reader["price"].ToString();
row.Cells.Add(cel);
cel=newTableCell();
TextBoxtxt=newTextBox();
txt.ID="txt"+reader["id"].ToString();
txt.Text="1";
txt.Width=40;
txt.MaxLength=3;
cel.Controls.Add(txt);
row.Cells.Add(cel);
cel=newTableCell();
Imageimg=newImage();
img.Width=60;
img.Height=100;
img.ImageUrl=reader["photoPath"].ToString();
cel.Controls.Add(img);
row.Cells.Add(cel);
cel=newTableCell();
Buttonbtn=newButton();
btn.Text="放入购物车";
btn.Command+=newCommandEventHandler(btn_Command);
btn.CommandArgument=reader["id"].ToString()+";"+reader["name"].ToString();
cel.Controls.Add(btn);
row.Cells.Add(cel);
t.Rows.Add(row);
}
voidbtn_Command(objectsender,CommandEventArgse)
{
string[]strs=e.CommandArgument.ToString().Split(';');
DataTablecart=Session["cart"]asDataTable;
if(cart==null)
cart=this.InitShoppingCart();
DataViewdv=newDataView(cart);
dv.RowFilter="id="+strs[0];
DataRowViewrow=null;
intnumNEW=1;
TextBoxtxt=this.FindControl("txt"+strs[0])asTextBox;
if(txt!=null)
{
numNEW=Convert.ToInt32(txt.Text);
}
if(dv.Count!=0)
{
row=dv[0];
intnums=(int)row["nums"];
row["nums"]=nums+numNEW;
}
else
{
row=dv.AddNew();
row["nums"]=numNEW;
row["id"]=strs[0];
row["name"]=strs[1];
}
row.EndEdit();
}
privateDataTableInitShoppingCart()
{
DataTabledt=newDataTable();
DataColumndc=newDataColumn("id",typeof(int));
dt.Columns.Add(dc);
dc=newDataColumn("name",typeof(string));
dt.Columns.Add(dc);
dc=newDataColumn("nums",typeof(int));
dt.Columns.Add(dc);
Session["cart"]=dt;
returndt;
}
privatevoidInitTitle(Tablet)
{
//t.Attributes.Add("rules","rows");
//t.BorderStyle=
t.BorderWidth=1;
TableRowrow=newTableRow();
TableCellcell=newTableCell();
cell.Text="书名";
row.Cells.Add(cell);
cell=newTableCell();
cell.Text="价格";
row.Cells.Add(cell);
cell=newTableCell();
cell.Text="图";
row.Cells.Add(cell);
cell=newTableCell();
cell.Text="";
row.Cells.Add(cell);
t.Rows.Add(row);
}
protectedvoidButton1_Click(objectsender,EventArgse)
{
this.Server.Transfer("viewCart.aspx");
}
}
viewCart.aspx:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Data;
publicpartialclassViewCart:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
DataTabledt=Session["cart"]asDataTable;
if(dt!=null)
{
this.GridView1.DataSource=dt;
this.GridView1.DataBind();
}
else
Response.Write("还没有购物!?");
}
}