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

时间:2015-07-21 07:40:43

标签: asp.net vb.net gridview code-behind

我有一个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

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

谢谢..

1 个答案:

答案 0 :(得分:0)

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

    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