?? spwh.aspx.cs
字號:
?using System;
using System.Data;
using System.Data.SqlClient;
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 spwh : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//默認(rèn)顯示商品資料的第一頁
bindgrig();
}
}
void bindgrig()
{
SqlConnection Conn;
// 設(shè)置數(shù)據(jù)庫連接
String strConn = ConfigurationManager.AppSettings["conn"];
Conn = new SqlConnection(strConn);
//從數(shù)據(jù)庫取查詢結(jié)果
SqlDataAdapter Cmd = new SqlDataAdapter("select 商品編號,商品名稱,規(guī)格,型號,數(shù)量 from 商品庫存 where 商品名稱 like '%" + this.TextBox1.Text + "%' order by 商品編號", Conn);
DataSet Ds = new DataSet();
Cmd.Fill(Ds, "商品資料");
//指定數(shù)據(jù)源
GridView1.DataSource = new DataView(Ds.Tables[0]);
//數(shù)據(jù)綁定
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
//查詢符合條件商品,默認(rèn)第一頁
GridView1.PageIndex = 0;
bindgrig();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//改變顯示頁面
GridView1.PageIndex = e.NewPageIndex;
bindgrig();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
//設(shè)置行編輯狀態(tài)
GridView1.EditIndex = e.NewEditIndex;
bindgrig();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//設(shè)置數(shù)據(jù)庫連接
SqlConnection Conn;
String strConn = ConfigurationManager.AppSettings["conn"];
Conn = new SqlConnection(strConn);
Conn.Open();
//刪除行處理
String sql = "delete from 商品庫存 where 商品編號='" + GridView1.DataKeys[e.RowIndex].Value.ToString()+ "'";
SqlCommand Comm = new SqlCommand(sql, Conn);
Comm.ExecuteNonQuery();
Conn.Close();
GridView1.EditIndex = -1;
bindgrig();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//設(shè)置數(shù)據(jù)庫連接
SqlConnection Conn;
String strConn = ConfigurationManager.AppSettings["conn"];
Conn = new SqlConnection(strConn);
Conn.Open();
//提交行修改
String sql = "update 商品庫存 set 商品名稱='" + ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text + "',規(guī)格='" + ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text + "',型號='" + ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3")).Text + "',數(shù)量='" + ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox4")).Text + "' where 商品編號='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
SqlCommand Comm = new SqlCommand(sql, Conn);
Comm.ExecuteNonQuery();
Conn.Close();
GridView1.EditIndex = -1;
bindgrig();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
//行編輯狀態(tài)取消
GridView1.EditIndex = -1;
bindgrig();
}
protected void Button2_Click(object sender, EventArgs e)
{
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -