2016-07-05 44 views
0

我在我的GridView中有一個ID爲「btnApprove」的按鈕。我想要的是當用戶單擊按鈕時,「狀態」行將更新爲「已批准」。我該如何實現這一目標?一行將只在點擊按鈕時更新,具體取決於交易編號如何通過一行按鈕更新一行點擊

這裏是我的aspx代碼。這裏

<asp:UpdatePanel ID="panel1" runat="server"> 
     <ContentTemplate> 
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="TransactionID" OnRowDataBound="GridView1_OnRowDataBound" OnRowCommand="GridView1_RowCommand" CellPadding="4" AllowPaging="true" PageIndex="2" OnPageIndexChanging="GridView1_PageIndexChanging" HeaderStyle-BackColor ="CornflowerBlue" BorderWidth="1" BorderColor="Gray" Width="100%" CssClass=" table table-hover" > 
     <Columns> 
      <asp:TemplateField> 
       <HeaderTemplate> 
        <asp:CheckBox ID="chkHeader" runat="server" /> 
       </HeaderTemplate> 
       <ItemTemplate> 
        <asp:CheckBox ID="chkSelect" runat="server" /> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField> 
       <ItemTemplate> 
        <img style="cursor:pointer" src ="../Images/Icons/plus2.png" /> 
        <asp:Panel ID ="pnlDetails" runat="server" Style="display: none"> 
         <asp:GridView ID="gvDet" runat="server" AutoGenerateColumns="false" CssClass="ChildGrid"> 
          <Columns> 
           <%--<asp:BoundField ItemStyle-Width="20px" DataField="ID" HeaderText="ID" />--%> 
           <asp:BoundField ItemStyle-Width="200px" DataField="ItemType" HeaderText="Type" /> 
           <asp:BoundField ItemStyle-Width="250px" DataField="ItemModel" HeaderText="Model" /> 
           <asp:BoundField ItemStyle-Width="140px" DataField="ItemQuantity" HeaderText="Requested Quantity" /> 
           <asp:BoundField ItemStyle-Width="80px" DataField="ItemUnit" HeaderText="Unit" /> 
           <asp:BoundField ItemStyle-Width="100px" DataField="ItemDate" HeaderText="Date Needed" /> 
           <asp:BoundField ItemStyle-Width="200px" DataField="ItemDesc" HeaderText="Description" /> 
           <%--<asp:BoundField ItemStyle-Width="80px" DataField="ItemStatus" HeaderText="Status" />--%> 
          </Columns> 
         </asp:GridView> 
        </asp:Panel> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:BoundField ItemStyle-Width="150px" DataField="TransactionID" HeaderText="Transaction Number" /> 
      <asp:BoundField ItemStyle-Width="150px" DataField="DateFiled" HeaderText ="Date Filed" /> 
      <asp:BoundField ItemStyle-Width="150px" DataField="ReqName" HeaderText="Name" /> 
      <asp:BoundField ItemStyle-Width="150px" DataField="ReqCompany" HeaderText="Company" /> 
      <asp:BoundField ItemStyle-Width="150px" DataField="ReqBranch" HeaderText="Branch" /> 
      <asp:BoundField ItemStyle-Width="150px" DataField="ReqBU" HeaderText="Business Unit" /> 
      <asp:BoundField ItemStyle-Width="150px" DataField="ReqDept" HeaderText="Department" /> 
      <asp:BoundField ItemStyle-Width="150px" DataField="ReqSection" HeaderText="Section" /> 
      <asp:BoundField ItemStyle-Width="150px" DataField="TransStatus" HeaderText="Status" /> 
      <asp:TemplateField> 
       <ItemTemplate> 
        <asp:Button ID="btnApprove" runat="server" Text="Approve" OnClick="btnApprove_Click" CssClass="btn btn-primary" /> 
       </ItemTemplate> 
      </asp:TemplateField> 

     </Columns> 
     <HeaderStyle BackColor="CornflowerBlue" /> 
    </asp:GridView> 
      </ContentTemplate> 
     <Triggers> 
      <asp:AsyncPostBackTrigger ControlID="GridView1" /> 
     </Triggers> 
     </asp:UpdatePanel> 

更新是我的GridView的我的數據源

public void showTable() 
    { 
     Utility u = new Utility(); 
     string conn = u.connect(); 
     SqlConnection connUser = new SqlConnection(conn); 
     SqlDataAdapter adp = new SqlDataAdapter("select * from MosefTransaction where TransStatus = 'Pending'", connUser); 
     DataTable dt = new DataTable(); 
     connUser.Open(); 
     adp.Fill(dt); 
     GridView1.DataSource = dt; 
     GridView1.DataBind(); 
    } 
+0

什麼是GridView的數據源? – Kramb

+0

嗨Kramb,剛剛更新我的問題,謝謝! –

+0

我會使用Linq to Sql更新該行。 – Kramb

回答

2

添加CommandArgument = '<%#Container.DataItemIndex%>' 您的按鈕,並在後面的代碼,你可以得到在Gridview的RowCommand事件上引發事件的行

<asp:Button ID="btnApprove" runat="server" Text="Approve" CommandName="ApproveTransaction" CommandArgument='<%# Container.DataItemIndex %>'/> 

在您的codebehind訂閱gridview的行命令事件。

protected void myGridView_RowCommand(object sender, GridViewCommandEventArgs e) 
     { 
      if (e.CommandName == "ApproveTransaction") 
      { 
       int index = Convert.ToInt32(e.CommandArgument); 
       GridViewRow row = gvInfo.Rows[index]; 

       string cellText = row.Cells[2].Text; 

       //Update your data in database here and rebind the gridview to updated data 

      } 
}