首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在使用边界字段时更新GridView

如何在使用边界字段时更新GridView
EN

Stack Overflow用户
提问于 2012-09-01 02:52:17
回答 3查看 19K关注 0票数 2

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

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

代码语言:javascript
复制
<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代码为:

代码语言:javascript
复制
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();

    }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-09-01 02:55:52

1)您必须在处理结束时调用GridView2.DataBind()

代码语言:javascript
复制
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

代码语言:javascript
复制
<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

票数 4
EN

Stack Overflow用户

发布于 2012-09-01 02:55:42

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

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

代码语言:javascript
复制
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!");
}

如果你在访问文本框时遇到问题,你可以改编这个例子

代码语言:javascript
复制
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!
}
票数 1
EN

Stack Overflow用户

发布于 2012-09-01 03:48:17

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

代码语言:javascript
复制
<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,@ParentId)匹配。在网格视图控件上,选择启用编辑。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12221055

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档