负数的货币格式 [英] Currency Format for Negative numbers

查看:329
本文介绍了负数的货币格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hai.,
在gridview中,eval函数绑定负值(例如:-20.90)表示显示带有前色的$(20.90)值为红色.请为此提供解决方案.





谢谢与问候.,
IdhayaRani.C

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

推荐答案

(20.90)与前景色是红色的.请为此提供解决方案.





谢谢与问候.,
IdhayaRani.C
(20.90) with forecolor is red.please give the solution for this .





Thanks & With Regards.,
IdhayaRani.C


在GridView的页脚中显示总计的示例

在此示例中,我将使用流行的Northwind数据库.这不是必需的,如果您尚未安装此数据库,则可以使用任何其他数据库,甚至可以创建一个只有一个表和几行的新数据库.唯一重要的是您有一个带有一个数字列的表.在此示例中,数字列"ProductSales"是浮动类型,其中包含几种产品的销售额. Web表单输出将如下所示:


将一个GridView和一个SqlDataSource控件从工具箱拖到Web窗体. SqlDataSource控件定义了数据源,并且GridView将显示数据.这是GridView和SqlDataSource控件的标记代码:
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:
<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 =" ">< ; /asp:SqlDataSource >
ConnectionStrings:NorthwindConnStr %>" SelectCommand="SELECT [ProductName] ,[ProductSales] FROM [NORTHWIND].[dbo].[Sales by Category] WHERE CategoryID = 1"></asp:SqlDataSource>


重要的是将ShowFooter属性设置为True,因为默认情况下不会显示Footer元素.此标记代码将从数据库加载数据并在GridView中显示记录,但是总值仍未显示.标准GridView没有某些属性可以自动计算聚合函数,我们不能仅使用标记来完成此任务.为了显示总数,我们需要使用自定义ASP.NET服务器端代码进行计算.

在这种方法中,我们将使用GridView RowDataBound事件.代码如下所示:


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:

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


这篇关于负数的货币格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆