DataView的使用
DataView对象可以用来筛选、排序、查询、修改(添加、修改、删除)DataTable对象中的数据。
下面是做购物车的程序:
Default.aspx:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Table t = new Table();
InitTitle(t);
string strcnn = ConfigurationManager.ConnectionStrings["cnnstring"].ConnectionString;
using (SqlConnection sqlCnn = new SqlConnection(strcnn))
{
SqlCommand sqlcmm = new SqlCommand();
sqlcmm.Connection = sqlCnn;
sqlcmm.CommandText = "select * from pros";
sqlCnn.Open();
using (SqlDataReader reader = sqlcmm.ExecuteReader())
{
if (reader != null)
{
while (reader.Read())
{
FillData(t, reader);
}
}
}
}
this.div1.Controls.Add(t);
}
private void FillData(Table t, SqlDataReader reader)
{
TableRow row = new TableRow();
TableCell cel = new TableCell();
cel.Text = reader["name"].ToString();
row.Cells.Add(cel);
cel = new TableCell();
cel.Text = reader["price"].ToString();
row.Cells.Add(cel);
cel = new TableCell();
TextBox txt = new TextBox();
txt.ID = "txt" + reader["id"].ToString();
txt.Text = "1";
txt.Width = 40;
txt.MaxLength = 3;
cel.Controls.Add(txt);
row.Cells.Add(cel);
cel = new TableCell();
Image img = new Image();
img.Width = 60;
img.Height = 100;
img.ImageUrl = reader["photoPath"].ToString();
cel.Controls.Add(img);
row.Cells.Add(cel);
cel = new TableCell();
Button btn = new Button();
btn.Text = "放入购物车";
btn.Command += new CommandEventHandler(btn_Command);
btn.CommandArgument = reader["id"].ToString() + ";" + reader["name"].ToString();
cel.Controls.Add(btn);
row.Cells.Add(cel);
t.Rows.Add(row);
}
void btn_Command(object sender, CommandEventArgs e)
{
string[] strs = e.CommandArgument.ToString().Split(';');
DataTable cart = Session["cart"] as DataTable;
if (cart == null)
cart = this.InitShoppingCart();
DataView dv = new DataView(cart);
dv.RowFilter = "id=" + strs[0];
DataRowView row = null;
int numNEW = 1;
TextBox txt = this.FindControl("txt" + strs[0]) as TextBox;
if (txt!=null)
{
numNEW = Convert.ToInt32(txt.Text);
}
if (dv.Count != 0)
{
row = dv[0];
int nums = (int)row["nums"];
row["nums"] = nums + numNEW;
}
else
{
row = dv.AddNew();
row["nums"] = numNEW;
row["id"] = strs[0];
row["name"] = strs[1];
}
row.EndEdit();
}
private DataTable InitShoppingCart()
{
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("id", typeof(int));
dt.Columns.Add(dc);
dc = new DataColumn("name", typeof(string));
dt.Columns.Add(dc);
dc = new DataColumn("nums", typeof(int));
dt.Columns.Add(dc);
Session["cart"] = dt;
return dt;
}
private void InitTitle(Table t)
{
//t.Attributes.Add("rules", "rows");
// t.BorderStyle =
t.BorderWidth = 1;
TableRow row = new TableRow();
TableCell cell = new TableCell();
cell.Text = "书名";
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = "价格";
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = "图";
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = "";
row.Cells.Add(cell);
t.Rows.Add(row);
}
protected void Button1_Click(object sender, EventArgs e)
{
this.Server.Transfer("viewCart.aspx");
}
}
viewCart.aspx:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class ViewCart : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = Session["cart"] as DataTable;
if (dt != null)
{
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
else
Response.Write("还没有购物!?");
}
}