2015-07-21 113 views
0

我有一个GridView,它是动态行的总和,并且每行都有删除按钮。如何在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 

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

谢谢..

+1

问题是什么呢? –

+0

我得到这个错误.... NullReferenceException被捕获 - 对象引用未设置为对象的实例。 – kasim

+1

@kasin:你在哪里得到它?使用调试器,找到此异常的原因非常有帮助。什么是'null','LblTotalAmount'? –

回答

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