2015-07-21 111 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