想要更新gridview的特定行的列

时间:2016-11-15 18:38:14

标签: c# asp.net

我在gridview中添加了一个名为“accept”的按钮域。我想在管理员点击接受时将bitfield的列'status'更新为该特定行的'1'。我正在研究c#

 <asp:GridView ID="GridView1" runat="server"   AutoGenerateColumns="False" runat="server" Width="1000px"

                  AllowPaging="True" PageSize="8" 
CssClass="Grid" AlternatingRowStyle-CssClass="alt" PagerStyle-CssClass="pgr" DataSourceID="SqlDataSource1"  AllowSorting="True">

    <Columns>
         <asp:ButtonField Text="Accept" CommandName="Update" runat="server"/>
        <asp:BoundField DataField="fname" HeaderText="fname" SortExpression="fname" />
        <asp:BoundField DataField="emailid" HeaderText="emailid" SortExpression="emailid" />
        <asp:BoundField DataField="contact" HeaderText="contact" SortExpression="contact" />
        <asp:BoundField DataField="type" HeaderText="type" SortExpression="type" />
        <asp:BoundField DataField="username" HeaderText="username" SortExpression="username" />
        <asp:BoundField DataField="password" HeaderText="password" SortExpression="password" />
    </Columns>
<PagerStyle CssClass="pgr"></PagerStyle>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbrubyConnectionString3 %>" SelectCommand="SELECT  seller.fname, seller.emailid, seller.contact,seller.type, login.username, login.password FROM seller INNER JOIN login ON seller.sid=login.lid where login.status= ORDER BY login.createdate ">

1 个答案:

答案 0 :(得分:0)

为GridView1_RowCommand添加事件处理程序,查找命令&#34;更新&#34;,并获取DataItem,以便知道要更新的项目,然后在数据库中更新它,然后重新绑定gridview。

您还可以进行ajax调用以更新值,这将更快(传输和接收的数据更少),并且不需要整页刷新,但这需要更多的工作来设置。 / p>

通过更改:

为您的命令添加标识符(CommandArgument)
<asp:ButtonField Text="Accept" CommandName="Update" runat="server"/>

为:

<asp:ButtonField Text="Accept" CommandName="Update" runat="server"
  CommandArgument="<%# Eval("emailid") %>"/>

通过更改:

添加事件处理程序
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" runat="server"
  Width="1000px" AllowPaging="True" PageSize="8" CssClass="Grid"
  AlternatingRowStyle-CssClass="alt" PagerStyle-CssClass="pgr"
  DataSourceID="SqlDataSource1"  AllowSorting="True">

为:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" runat="server"
  Width="1000px" AllowPaging="True" PageSize="8" CssClass="Grid"
  AlternatingRowStyle-CssClass="alt" PagerStyle-CssClass="pgr"
  DataSourceID="SqlDataSource1"  AllowSorting="True"
  OnRowCommand="GridView1_OnRowCommand">

然后通过添加:

在后端编写更新
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
  switch(e.CommandName)
  {
    case "Accept":
      var email = e.CommandArgument.ToString();
      Accept(email); // write this function
      break;
  }
}