using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Update : System.Web.UI.Page
{
static string strconn = "Data Source=localhost;database=BOOKS;integrated security=SSPI";
SqlConnection conn = new SqlConnection(strconn);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadGridView();
}
}
private void LoadGridView()
{
ConnectionDB();
string SQL = "SELECT * FROM BOOKSTORAGE";
SqlCommand cmd = conn.CreateCommand();
SqlDataAdapter adaper = new SqlDataAdapter(SQL, strconn);
DataSet dataset = new DataSet();
adaper.Fill(dataset, "BOOKSTORAGE");
CloseConnection();
GVUpdate.DataSource = dataset.Tables["BOOKSTORAGE"];
GVUpdate.DataBind();
}
protected void GVUpdate_RowEditing(object sender, GridViewEditEventArgs e)
{
GVUpdate.EditIndex = e.NewEditIndex;
LoadGridView();
}
protected void GVUpdate_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GVUpdate.EditIndex = -1;
LoadGridView();
}
protected void GVUpdate_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//使用e.RowIndex获取当前变化行
//使用GVUpdate.Rows[e.RowIndex].Cells[0].Text获取id值
int CategoryID = Convert.ToInt32(GVUpdate.Rows[e.RowIndex].Cells[0].Text);
//获取当前编辑页面中的TextBox对象,其中内容为用户新输入的数据
TextBox Tempbk_name = (TextBox)GVUpdate.Rows[e.RowIndex].Cells[1].Controls[0];
TextBox Tempbk_author = (TextBox)GVUpdate.Rows[e.RowIndex].Cells[2].Controls[0];
TextBox Tempprice = (TextBox)GVUpdate.Rows[e.RowIndex].Cells[3].Controls[0];
TextBox bk_content=(TextBox)GVUpdate.Rows[e.RowIndex].Cells[4].Controls[0];
string bk_name = Tempbk_name.Text;
string bk_author=Tempbk_author.Text;
decimal price = Convert.ToDecimal(Tempprice.Text);
string bkcontent = bk_content.Text;
GVUpdate.EditIndex = -1;//由于网格中的索引值从0开始,只需将GridView控件的EditIndex属性设置为-1,即可取消编辑
ConnectionDB();
string SelectSql = "SELECT * FROM BOOKSTORAGE";
SqlDataAdapter adapter = new SqlDataAdapter(SelectSql, conn);
DataSet dataset = new DataSet();
adapter.Fill(dataset, "BOOKSTORAGE");
DataTable table = dataset.Tables["BOOKSTORAGE"];
//设置主鍵码
table.PrimaryKey = new DataColumn[] { table.Columns["id"] };
//用Find方法查找要修改的行
DataRow Rowbk_name = table.Rows.Find(CategoryID);
DataRow Rowbk_author = table.Rows.Find(CategoryID);
DataRow Rowprice = table.Rows.Find(CategoryID);
DataRow Rowcontent = table.Rows.Find(CategoryID);
//进行修改数据
Rowbk_name["bk_name"] = bk_name;
Rowbk_author["bk_author"] = bk_author;
Rowbk_author["price"] = price;
Rowcontent["bk_content"] = bkcontent;
//创建CommandBuilder对象
SqlCommandBuilder myCB = new SqlCommandBuilder(adapter);
SqlCommand CMD= myCB.GetDeleteCommand();
//将修改后的数据表推入到数据庫中
adapter.Update(dataset.Tables["BOOKSTORAGE"]);
CloseConnection();
LoadGridView();
}
private void ConnectionDB()
{
if (conn == null)
{
conn = new SqlConnection(strconn);
}
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
}
private void CloseConnection()
{
conn.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Manager.aspx");
}
}
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Update : System.Web.UI.Page
{
static string strconn = "Data Source=localhost;database=BOOKS;integrated security=SSPI";
SqlConnection conn = new SqlConnection(strconn);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadGridView();
}
}
private void LoadGridView()
{
ConnectionDB();
string SQL = "SELECT * FROM BOOKSTORAGE";
SqlCommand cmd = conn.CreateCommand();
SqlDataAdapter adaper = new SqlDataAdapter(SQL, strconn);
DataSet dataset = new DataSet();
adaper.Fill(dataset, "BOOKSTORAGE");
CloseConnection();
GVUpdate.DataSource = dataset.Tables["BOOKSTORAGE"];
GVUpdate.DataBind();
}
protected void GVUpdate_RowEditing(object sender, GridViewEditEventArgs e)
{
GVUpdate.EditIndex = e.NewEditIndex;
LoadGridView();
}
protected void GVUpdate_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GVUpdate.EditIndex = -1;
LoadGridView();
}
protected void GVUpdate_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//使用e.RowIndex获取当前变化行
//使用GVUpdate.Rows[e.RowIndex].Cells[0].Text获取id值
int CategoryID = Convert.ToInt32(GVUpdate.Rows[e.RowIndex].Cells[0].Text);
//获取当前编辑页面中的TextBox对象,其中内容为用户新输入的数据
TextBox Tempbk_name = (TextBox)GVUpdate.Rows[e.RowIndex].Cells[1].Controls[0];
TextBox Tempbk_author = (TextBox)GVUpdate.Rows[e.RowIndex].Cells[2].Controls[0];
TextBox Tempprice = (TextBox)GVUpdate.Rows[e.RowIndex].Cells[3].Controls[0];
TextBox bk_content=(TextBox)GVUpdate.Rows[e.RowIndex].Cells[4].Controls[0];
string bk_name = Tempbk_name.Text;
string bk_author=Tempbk_author.Text;
decimal price = Convert.ToDecimal(Tempprice.Text);
string bkcontent = bk_content.Text;
GVUpdate.EditIndex = -1;//由于网格中的索引值从0开始,只需将GridView控件的EditIndex属性设置为-1,即可取消编辑
ConnectionDB();
string SelectSql = "SELECT * FROM BOOKSTORAGE";
SqlDataAdapter adapter = new SqlDataAdapter(SelectSql, conn);
DataSet dataset = new DataSet();
adapter.Fill(dataset, "BOOKSTORAGE");
DataTable table = dataset.Tables["BOOKSTORAGE"];
//设置主鍵码
table.PrimaryKey = new DataColumn[] { table.Columns["id"] };
//用Find方法查找要修改的行
DataRow Rowbk_name = table.Rows.Find(CategoryID);
DataRow Rowbk_author = table.Rows.Find(CategoryID);
DataRow Rowprice = table.Rows.Find(CategoryID);
DataRow Rowcontent = table.Rows.Find(CategoryID);
//进行修改数据
Rowbk_name["bk_name"] = bk_name;
Rowbk_author["bk_author"] = bk_author;
Rowbk_author["price"] = price;
Rowcontent["bk_content"] = bkcontent;
//创建CommandBuilder对象
SqlCommandBuilder myCB = new SqlCommandBuilder(adapter);
SqlCommand CMD= myCB.GetDeleteCommand();
//将修改后的数据表推入到数据庫中
adapter.Update(dataset.Tables["BOOKSTORAGE"]);
CloseConnection();
LoadGridView();
}
private void ConnectionDB()
{
if (conn == null)
{
conn = new SqlConnection(strconn);
}
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
}
private void CloseConnection()
{
conn.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Manager.aspx");
}
}