2

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

GridView我绑定到 SQLdatasource 后的 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>

我的数据键是 Locations 并且它是只读的。

用于更新的 .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();

    }
4

3 回答 3

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

于 2012-08-31T18:55:52.357 回答
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!
}
于 2012-08-31T18:55:41.953 回答
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>

我已经测试了这段代码,它会读取表格,并允许编辑记录。值得关注的点。确保您有更新查询。确保更新中的参数与字段名称(@branchName、@ParentId)匹配。在 gridview 控件上选择启用编辑。

于 2012-08-31T19:48:16.603 回答