private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)//修改的代码
{
try
{
string pr_oduct_id = ((TextBox)e.Item.Cells[2].Controls[0]).Text;//取得DataGrid1的选定行的每单元数据
string pr_oductname = ((TextBox)e.Item.Cells[3].Controls[0]).Text;;//这种做法,使用了e这个变量,这里的e
string pr_ovider = ((TextBox)e.Item.Cells[4].Controls[0]).Text;//不同于Button2_Click的e,所以,在Button2_Click里面
string pr_ice = ((TextBox)e.Item.Cells[5].Controls[0]).Text;//不能使用这种方法得到DataGrid中的数据
string un_it = ((TextBox)e.Item.Cells[6].Controls[0]).Text;
string qt_y = ((TextBox)e.Item.Cells[7].Controls[0]).Text;
string st_ock_date = ((TextBox)e.Item.Cells[8].Controls[0]).Text;//取得DataGrid1的选定行的每单元数据
if(pr_oductname!="" && pr_ovider!=""&& pr_ice!=""&& un_it!=""&& qt_y!="" && st_ock_date!="")//防止输入的为空数据
{
SqlConnection connUpdate = new SqlConnection(strCnn);
connUpdate.Open();
String sql_edit = "update T_TRA_PRODUCT set product_name = '" + pr_oductname.Replace("''","''''") + "'," +
"provider = '" + pr_ovider.Replace("''","''''") + "'," +
"price = '" + pr_ice.Replace("''","''''") + "'," +
"unit = '" + un_it.Replace("''","''''") + "' ," +
"qty = '" + qt_y.Replace("''","''''") + "' ," +
"stock_date = '" + st_ock_date.Replace("''","''''") + "' " +
"WHERE product_id ='" + pr_oduct_id + "'";//注意此句修改语句中的Replace的用法,他能够让新的数据代替旧的数据
//注意里面有许多引号的使用
SqlCommand sqlCommandUpdate = new SqlCommand(sql_edit,connUpdate);
sqlCommandUpdate.ExecuteNonQuery();
connUpdate.Close();
sql = "Select * FROM T_TRA_PRODUCT";
DataGrid1.EditItemIndex = -1;
DataGrid1.DataSource = CreateDataSource();
DataGrid1.DataBind();
}
}
catch(Exception ee)
{
}
}