Jes Sie

Jes Sie

  • 744
  • 1.2k
  • 281k

Add two columns value and show total in another column

Mar 20 2017 11:02 PM
I have a gridview with 9 columns. I want to get the sum of column Net Premium, VAT & Registry Fee. I added another template field called TOTAL. Now, I want to display the sum result in the TOTAL column. But I got this error... attached also the snippets of my front and backend code.
 
  1. <asp:GridView ID="gvListOfIssuedTaxInvoice" runat="server" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px"  
  2.                         CellPadding="4" AutoGenerateColumns="False" ShowFooter="True" OnRowDataBound="gvListOfIssuedTaxInvoice_RowDataBound" DataSourceID="SqlDataSource1">  
  3.                         <Columns>  
  4.                             <asp:TemplateField HeaderText="Tax Invoice #" SortExpression="TaxInvoiceNumber">  
  5.                                 <EditItemTemplate>  
  6.                                     <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("TaxInvoiceNumber") %>'></asp:TextBox>  
  7.                                 </EditItemTemplate>  
  8.                                 <ItemTemplate>  
  9.                                     <asp:Label ID="Label1" runat="server" Text='<%# Bind("TaxInvoiceNumber") %>'></asp:Label>  
  10.                                 </ItemTemplate>  
  11.                             </asp:TemplateField>  
  12.                             <asp:TemplateField HeaderText="Date" SortExpression="TransactionDate">  
  13.                                 <EditItemTemplate>  
  14.                                     <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("TransactionDate") %>'></asp:TextBox>  
  15.                                 </EditItemTemplate>  
  16.                                 <ItemTemplate>  
  17.                                     <asp:Label ID="Label2" runat="server" Text='<%# Bind("TransactionDate") %>'></asp:Label>  
  18.                                 </ItemTemplate>  
  19.                             </asp:TemplateField>  
  20.                             <asp:TemplateField HeaderText="Book #" SortExpression="BookNumber">  
  21.                                 <EditItemTemplate>  
  22.                                     <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("BookNumber") %>'></asp:TextBox>  
  23.                                 </EditItemTemplate>  
  24.                                 <ItemTemplate>  
  25.                                     <asp:Label ID="Label3" runat="server" Text='<%# Bind("BookNumber") %>'></asp:Label>  
  26.                                 </ItemTemplate>  
  27.                             </asp:TemplateField>  
  28.                             <asp:TemplateField HeaderText="Customer Name" SortExpression="CustomerName">  
  29.                                 <EditItemTemplate>  
  30.                                     <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("CustomerName") %>'></asp:TextBox>  
  31.                                 </EditItemTemplate>  
  32.                                 <ItemTemplate>  
  33.                                     <asp:Label ID="Label4" runat="server" Text='<%# Bind("CustomerName") %>'></asp:Label>  
  34.                                 </ItemTemplate>  
  35.                             </asp:TemplateField>  
  36.                             <asp:TemplateField HeaderText="Province" SortExpression="Province">  
  37.                                 <EditItemTemplate>  
  38.                                     <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Province") %>'></asp:TextBox>  
  39.                                 </EditItemTemplate>  
  40.                                 <ItemTemplate>  
  41.                                     <asp:Label ID="Label5" runat="server" Text='<%# Bind("Province") %>'></asp:Label>  
  42.                                 </ItemTemplate>  
  43.                             </asp:TemplateField>  
  44.                             <asp:TemplateField HeaderText="Net Premium" SortExpression="NetPremium">  
  45.                                 <EditItemTemplate>  
  46.                                     <asp:Label ID="Label1" runat="server" Text='<%# Eval("NetPremium") %>'></asp:Label>  
  47.                                 </EditItemTemplate>  
  48.                                 <ItemTemplate>  
  49.                                     <asp:Label ID="Label6" runat="server" Text='<%# Bind("NetPremium") %>'></asp:Label>  
  50.                                 </ItemTemplate>  
  51.                             </asp:TemplateField>  
  52.                             <asp:TemplateField HeaderText="VAT" SortExpression="VAT">  
  53.                                 <EditItemTemplate>  
  54.                                     <asp:Label ID="Label2" runat="server" Text='<%# Eval("VAT") %>'></asp:Label>  
  55.                                 </EditItemTemplate>  
  56.                                 <ItemTemplate>  
  57.                                     <asp:Label ID="Label7" runat="server" Text='<%# Bind("VAT") %>'></asp:Label>  
  58.                                 </ItemTemplate>  
  59.                             </asp:TemplateField>  
  60.                             <asp:TemplateField HeaderText="Registry Fee" SortExpression="RegistryFee">  
  61.                                 <EditItemTemplate>  
  62.                                     <asp:Label ID="Label3" runat="server" Text='<%# Eval("RegistryFee") %>'></asp:Label>  
  63.                                 </EditItemTemplate>  
  64.                                 <ItemTemplate>  
  65.                                     <asp:Label ID="Label8" runat="server" Text='<%# Bind("RegistryFee") %>'></asp:Label>  
  66.                                 </ItemTemplate>  
  67.                             </asp:TemplateField>  
  68.                             <asp:TemplateField HeaderText="TOTAL">  
  69.                                 <ItemTemplate>  
  70.                                     <asp:Label ID="lblTOTAL" runat="server" ></asp:Label>  
  71.                                 </ItemTemplate>  
  72.                             </asp:TemplateField>  
  73.                         </Columns>  
  74.                         <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />  
  75.                         <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />  
  76.                         <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />  
  77.                         <RowStyle BackColor="White" ForeColor="#003399" />  
  78.                         <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />  
  79.                         <SortedAscendingCellStyle BackColor="#EDF6F6" />  
  80.                         <SortedAscendingHeaderStyle BackColor="#0D4AC4" />  
  81.                         <SortedDescendingCellStyle BackColor="#D6DFDF" />  
  82.                         <SortedDescendingHeaderStyle BackColor="#002876" />  
  83.                     </asp:GridView>  
  84.   
  85.   
  86.  protected void gvListOfIssuedTaxInvoice_RowDataBound(object sender, GridViewRowEventArgs e)  
  87.         {  
  88.             if (e.Row.RowType == DataControlRowType.DataRow)  
  89.             {  
  90.                 decimal netpremium = Convert.ToDecimal(e.Row.Cells[6].Text);  
  91.                 decimal vat = Convert.ToDecimal(e.Row.Cells[7].Text);  
  92.                 decimal registry = Convert.ToDecimal(e.Row.Cells[8].Text);  
  93.                 Label lblTOTAL = (Label)e.Row.Cells[9].FindControl("lblTOTAL");  
  94.                   
  95.                 decimal sum = netpremium + vat + registry;  
  96.                 lblTOTAL.Text += sum.ToString();  
  97.             }  
  98.         }  
 

Answers (4)