如何在 vb.net 中动态求和 gridview 列值?

How to sum gridview column values dynamically in vb.net?

我有一个 Gridview,它是动态行的总和,并且每行都有删除按钮。

WebForm1.aspx

<asp:GridView ID="grdView1" runat="server" AutoGenerateColumns="false" ShowFooter="true">
<Columns>
    <asp:TemplateField HeaderText="S.No">
        <ItemTemplate>
           <asp:Label ID="LblSno" runat="server" Text='<%#Container.DataItemIndex+1 %>'></asp:Label>
        </ItemTemplate>
        <ItemStyle HorizontalAlign="center" BorderWidth="1px" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Particulars">
        <ItemTemplate>
            <asp:DropDownList ID="drpParticulars" runat="server" >
            </asp:DropDownList>
        </ItemTemplate>
        <FooterTemplate>
             <asp:Label ID="lblTotal" runat="server">Total :</asp:Label>
        </FooterTemplate>
        <ItemStyle HorizontalAlign="Left" BorderWidth="1px" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Amount">
        <ItemTemplate>
            <asp:TextBox ID="txtAmount" runat="server" OnTextChanged="txtAmount_TextChanged" AutoPostBack="true"></asp:TextBox>
         </ItemTemplate>
         <FooterTemplate>
            <asp:Label ID="lblTotalAmount" runat="server"></asp:Label>
         </FooterTemplate>
         <ItemStyle HorizontalAlign="Left" BorderWidth="1px" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Edit Record">
        <ItemTemplate>
            <asp:ImageButton ID="ImgDelete" runat="server" CausesValidation="false" CommandName="delete" ImageUrl="~/images/cancel.png" ToolTip="Delete Record" OnClientClick="return confirm('Are you sure?');" Text="Delete" />
        </ItemTemplate>
        <FooterTemplate>
            <asp:ImageButton ID="ImgAddNewRow" runat="server" CssClass="gridimage" OnClick="addnew_Click" ImageUrl="~/images/addnew.png" ToolTip="Add New Row" Text="Add Row" />
        </FooterTemplate>
        <ItemStyle HorizontalAlign="Center" />
   </asp:TemplateField>
   </Columns>
   <FooterStyle CssClass="header_Order" />
</asp:GridView>

我的预期结果:

S.No   Particulars   Amount   Edit Record
-----  -----------   ------   -----------
 1      item Cr       2500         X
 2      item Cr       1500         X
 3      item Dr       3000         X 
-----------------------------------------
        Total         7000         +
-----------------------------------------

现在我的问题是所有行值的总和,而 Text_Changed 本身。

WebForm1.aspx.vb

Protected Sub txtAmount_TextChanged(sender As Object, e As System.EventArgs)
  Dim tmpval As Integer = 0
  For i As Integer = 0 To grdView1.Rows.Count - 1
     Dim TxtAmount As TextBox = DirectCast(grdView1.Rows(i).FindControl("txtAmount"), TextBox)
     Dim LblTotalAmount As Label = DirectCast(grdView1.Rows(i).FindControl("lblTotalAmount"), Label)

     tmpval = tmpval + Integer.Parse(TxtAmount.Text)
     LblTotalAmount.Text = tmpval.ToString()
  Next
End Sub

当光标超出文本框时,它会自动对行值求和并将结果绑定到页脚标签列。我已经在堆栈溢出中彻底搜索了我的解决方案,但不适合我的问题。所以只有我问这个问题。

谢谢..

我以另一种方式找到了我的结果。我用一行创建了单独的 table 以获得总值的总和。它很好,工作正常。

    S.No   Particulars   Amount   Edit Record
    -----  -----------   ------   -----------
     1      item1 Cr       2500         X
     2      item2 Cr       1500         X
     3      item3 Dr       3000         X 
    -----------------------------------------
                                        +
    -----------------------------------------
            Total          7000         
    -----------------------------------------

WebForm1.aspx.vb

Protected Sub txtAmount_TextChanged(sender As Object, e As System.EventArgs)
  Dim tmpval As Integer = 0
  Dim tmpCalVal As Integer = 0
  For i As Integer = 0 To grdView1.Rows.Count - 1
     Dim TxtAmount As TextBox = DirectCast(grdView1.Rows(i).FindControl("txtAmount"), TextBox)

     tmpCalVal = Convert.ToInt32(TxtAmount.Text)
     tmpval = tmpval + (tmpCalVal)
     lblTotalPaymentVal.Text = tmpval.ToString()
  Next
End Sub