Showing posts with label How to Display Sum Total in the Footer of the GridView Control?. Show all posts
Showing posts with label How to Display Sum Total in the Footer of the GridView Control?. Show all posts

How to display decimal values in a gridview?


Our main aim to display sum total as shown in fig above

Display Sum Total in Footer of GridView Control

<asp:GridView ID="GridView1"

ShowFooter="true" DataKeyNames="ProductId"
AutoGenerateColumns="false" runat="server"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="Productid" HeaderText="Product Id" />
<asp:BoundField DataField="ProductName" FooterText="Total" HeaderText="Product Name" />
<asp:TemplateField HeaderText="Unit Price" FooterStyle-Font-Bold="True">
<ItemTemplate>
<%# GetUnitPrice(decimal.Parse(Eval("UnitPrice").ToString())).ToString("N2") %>
</ItemTemplate>
<FooterTemplate>
<%# GetTotal().ToString("N2") %>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DummyDB %>" SelectCommand="Select * from Products"> </asp:SqlDataSource>
Finally, we'll use Helper functions named GetUnitPrice and GetTotal to display the UnitPrice in the ItemTemplate and Total in the FooterTemplate. For instance, for each row of the GridView, a price value is passed to the GetUnitPrice function returning variable Price

Using Helper Functions
decimal TotalUnitPrice;

decimal GetUnitPrice(decimal Price)
{
TotalUnitPrice += Price;
return Price;
}
decimal GetTotal()
{
return TotalUnitPrice;
}
if i have a number and i wanted to formatt to 5 decimals i can use the following syntax
int val = 28;

string formatted = val.ToString("N5");

Template by - Mathew | Mux99