如何在使用boundfield时更新GridView

我有一个GridView ,它绑定到一个数据库。 我发现在更新GridView和数据库中的相应表时遇到了困难。

绑定到SQLdatasource后,我的GridView asp代码是:

         <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="" SelectCommand="SELECT * FROM [Quantity]">  

我的数据键是Locations及其readonly。

用于更新的.cs代码是:

 protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e) { SqlConnection con = new SqlConnection("Data Source=ARCHANA-PC\\ARCHANA;Initial Catalog=TouchPad;Integrated Security=True"); string LocName = GridView2.DataKeys[e.RowIndex].Values["Locations"].ToString(); TextBox txt1 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("Lamp_pro4"); TextBox txt2 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("Lamp_pro5"); TextBox txt3 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("AC_Profile5"); string updStmt = "UPDATE Quantity set Lamp_pro4=@Lamp_pro4,Lamp_pro5=@Lamp_pro5,AC_Profile5=@AC_Profile5 where Locations=@locName"; con.Open(); SqlCommand updCmd = new SqlCommand(updStmt, con); updCmd.Parameters.AddWithValue("@locName", LocName); updCmd.Parameters.AddWithValue("@Lamp_pro4", txt1.Text); updCmd.Parameters.AddWithValue("@Lamp_pro5", txt2.Text); updCmd.Parameters.AddWithValue("@AC_Profile5", txt3.Text); updCmd.ExecuteNonQuery(); GridView2.DataBind(); } 

1)您必须在治疗结束时调用GridView2.DataBind()

 protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e) { //GridViewRow row = (GridViewRow)GridView2.Rows[e.RowIndex]; string LocName = GridView2.DataKeys[e.RowIndex].Values["Locations"].ToString(); TextBox txt1 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("Lamp_pro4"); TextBox txt2 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("Lamp_pro5"); TextBox txt3 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("AC_Profile5"); con.Open(); SqlCommand cmd = new SqlCommand("UPDATE Quantity set Lamp_pro4='" + txt1.Text + "',Lamp_pro5='" + txt2.Text + "',AC_Profile5='" + txt3.Text + "' where Locations=" + LocName, con); cmd.ExecuteNonQuery(); con.Close(); GridView2.EditIndex = -1; //BindQuantity(); GridView2.DataBind(); } 

2)在SqlDataSource上定义UpdateCommand

   

链接: http : //msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.sqldatasource.updatecommand.aspx

只需调用GridView2.DataBind()将数据重新绑定到控件上。

无论如何你应该真的使用SQL参数来防止SQL注入! 看看这个例子

 string connetionString = "YOUR_CONSTR" ; string updStmt = "UPDATE Quantity set Lamp_pro4=@lamp_pro4,Lamp_pro5=@Lamp_pro5,AC_Profile5=@ac_profile5 " + "where Locations=@locName"; using (SqlConnection cnn = new SqlConnection(connetionString)) { cnn.Open(); SqlCommand updCmd = new SqlCommand(updStmt , cnn); // use sqlParameters to prevent sql injection! updCmd.Parameters.AddWithValue("@lamp_pro4", txt1.Text); // or define dataType if necessary SqlParameter p1 = new SqlParameter(); p1.ParameterName = "@Lamp_pro5"; p1.SqlDbType = SqlDbType.Int; p1.Value = txt2.Text; updCmd.Parameters.Add(p1); // demo code must be adapted!! (correct paramNames, textbox names, add missing params ...) int affectedRows = updCmd.ExecuteNonQuery(); Debug.WriteLine(affectedRows + " rows updated!"); } 

如果您在访问文本框时遇到问题,可以调整此示例

 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { string Lamp_pro4 = e.NewValues["Lamp_pro4"].ToString(); Debug.WriteLine("Lamp_pro4: " + Lamp_pro4); // to check if everything works fine! } 

使用数据绑定网格视图和绑定列不需要代码。 ASPX标记为您完成所有更新。

           

我测试了这段代码,它读取了表格,并允许对记录进行编辑。 点到warch为。 确保您有更新查询。 确保更新上的参数与字段名称匹配(@ branchName,@ PlainId)。 在gridview控件上,选择“启用编辑”。