我有一个GridView
,它绑定到一个数据库。我发现在更新数据库中的GridView
和相应表时遇到了困难。
绑定到SQLdatasource后,我的GridView
的asp代码是:
<asp:GridView ID="GridView2" runat="server" OnRowEditing="GridView2_RowEditing"
OnRowUpdating="GridView2_RowUpdating" CellPadding="4" ForeColor="#333333" OnRowCancelingEdit="GridView2_RowCancelingEdit"
OnRowDataBound="GridView2_RowDataBound" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" AutoGenerateEditButton="True" DataKeyNames="Locations">
<Columns>
<asp:BoundField DataField="Locations" HeaderText="Locations"
SortExpression="Locations" ReadOnly="true"/>
<asp:BoundField DataField="Lamp_pro4" HeaderText="Lamp_pro4"
SortExpression="Lamp_pro4" />
<asp:BoundField DataField="Lamp_pro5" HeaderText="Lamp_pro5"
SortExpression="Lamp_pro5" />
<asp:BoundField DataField="AC_Profile5" HeaderText="AC_Profile5"
SortExpression="AC_Profile5" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TouchPadConnectionString %>"
SelectCommand="SELECT * FROM [Quantity]">
</asp:SqlDataSource>
我的数据键是位置及其只读。
用于更新的.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();
}
答案 0 :(得分:4)
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)在UpdateCommand
SqlDataSource
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TouchPadConnectionString %>"
SelectCommand="SELECT * FROM [Quantity]"
UpdateCommand="UPDATE Quantity set Lamp_pro4 = @Lamp_pro4 ,Lamp_pro5=@Lamp_pro5,AC_Profile5=@AC_Profile5 where Locations=@Locations">
</asp:SqlDataSource>
链接:http://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.sqldatasource.updatecommand.aspx
答案 1 :(得分:1)
只需致电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!
}
答案 2 :(得分:0)
使用数据绑定网格视图和绑定列不需要代码。 ASPX标记为您完成所有更新。
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="recid" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="recid" HeaderText="recid" InsertVisible="False" ReadOnly="True" SortExpression="recid" />
<asp:BoundField DataField="BranchName" HeaderText="BranchName" SortExpression="BranchName" />
<asp:BoundField DataField="ParentId" HeaderText="ParentId" SortExpression="ParentId" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ZA_testConnectionString %>"
SelectCommand="SELECT * FROM [Branch]"
UpdateCommand="UPDATE [BRANCH] SET BranchName=@BranchName , ParentId=@ParentId WHERE recid=@recid">
</asp:SqlDataSource>
我测试了这段代码,它读取了表格,并允许对记录进行编辑。点到warch为。确保您有更新查询。确保更新上的参数与字段名称匹配(@ branchName,@ PlainId)。在gridview控件上,选择“启用编辑”。