繁体   English   中英

如何在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

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

谢谢..

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

    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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM