Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai.,
In gridview eval function bind negative values (ex: -20.90) means display that values to $(20.90) with forecolor is red.please give the solution for this .





Thanks & With Regards.,
IdhayaRani.C
Posted

Example of showing total in GridView's footer

In this example, I will use popular Northwind database. It is not required, if you don't have this database already installed, you can use any other database or even create new one with one table and few rows. Only thing important is that you have a table with one numeric column. In this example, numeric column "ProductSales" is type of float and contains sales amounts for several products. Web form output will look like this:


Drag one GridView and one SqlDataSource control from toolbox to the web form. SqlDataSource control defines data source and GridView will show the data. Here is the markup code of GridView and SqlDataSource control:
XML
<asp:GridView ID="GridView1" runat="server" DataSourceID="sdsNorthwind" 
  AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" 
  GridLines="None" ShowFooter="True" onrowdatabound="GridView1_RowDataBound">
  
  <%--This example consists of two columns, second column is numeric and formatted as currency--%>
  <Columns>
  <asp:BoundField DataField="ProductName" HeaderText="Product Name" FooterText="Total:" />
  <asp:BoundField DataField="ProductSales" HeaderText="Product Sales" 
  DataFormatString="{0:c}" />
  </Columns>
  
  <%--Set styles to get better GridView appearance--%>
  <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
  <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
  <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
  </asp:GridView>
  
  <%--SqlDataSource control opens [Sales by Category] view of Northwind database--%>
  <asp:SqlDataSource ID="sdsNorthwind" runat="server" 
  ConnectionString="<%$ ConnectionStrings:NorthwindConnStr %>" 
  SelectCommand="SELECT [ProductName] ,[ProductSales]
   FROM [NORTHWIND].[dbo].[Sales by Category] WHERE CategoryID = 1"></asp:SqlDataSource>

Important thing is to set ShowFooter property to True because by default Footer element is not displayed. This markup code will load data from database and show records in GridView, but Total value is still not displayed. Standard GridView hasn't some property to calculate aggregate functions automatically and we can't finish this task using only markup. To show total, we need to calculate it using custom ASP.NET server side code.

In this approach, we'll use GridView RowDataBound event. Code would look like this:
VB
Partial Class GridView_Total_VB
  Inherits System.Web.UI.Page
  
    ' Declare variable used to store value of Total
    Private TotalSales As Decimal = 0.0
  
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
      ' check row type
      If e.Row.RowType = DataControlRowType.DataRow Then
        ' if row type is DataRow, add ProductSales value to TotalSales
        TotalSales += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "ProductSales"))
      ElseIf e.Row.RowType = DataControlRowType.Footer Then
        ' If row type is footer, show calculated total value
        ' Since this example uses sales in dollars, I formatted output as currency
        e.Row.Cells(1).Text = String.Format("{0:c}", TotalSales)
      End If
    End Sub
  End Class
 
Share this answer
 
v2
Comments
fjdiewornncalwe 22-Feb-12 10:09am    
Just updated your answer to use pre tags around the source blocks.
Hi, First check your no. is negative or not. if it is negative then remove this and send other value to change word format. and for negative values add extra variable for defining, it is negative.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900