在Asp.Net 2.0中应用DataFormatString

 DataFormatString 属性语法如下:

DataFormatString="{0:格式字符串}"

我们知道在DataFormatString 中的 {0} 表示数据本身,而在冒号后面的格式字符串代表所们希望数据显示的格式;另外在指定的格式符号后可以指定小数所要显示的位数。例如原来的数据为「1.56」,若格式设定为 {0:N1},则输出为「1.5」。其常用的数值格式如下表所示:

格式字符串   输入                         结果
"{0:C}"           12345.6789           $12,345.68
"{0:C}"           -12345.6789           ($12,345.68)
"{0:D}"           12345                     12345
"{0:D8}"         12345                     00012345
"{0:E}"            12345.6789          1234568E+004
"{0:E10}"       12345.6789           1.2345678900E+004
"{0:F}"            12345.6789           12345.68
"{0:F0}"          12345.6789          12346
"{0:G}"           12345.6789           12345.6789
"{0:G7}"         123456789            1.234568E8
"{0:N}"           12345.6789           12,345.68
"{0:N4}"         123456789           123,456,789.0000
"Total: {0:C}" 12345.6789         Total: $12345.68

其常用的日期格式如下表所示:

格式        说明                       输出格式
d             精简日期格式        MM/dd/yyyy
D            详细日期格式        dddd, MMMM dd, yyyy
f              完整格式                (long date + short time) dddd, MMMM dd, yyyy HH:mm
F            完整日期时间格式 (long date + long time)  dddd, MMMM dd, yyyy HH:mm:ss
g            一般格式                (short date + short time) MM/dd/yyyy HH:mm
G           一般格式                 (short date + long time) MM/dd/yyyy HH:mm:ss
m,M      月日格式                  MMMM dd
s           适中日期时间格式 yyyy-MM-dd HH:mm:ss
t            精简时间格式          HH:mm
T          详细时间格式           HH:mm:ss

注意:在ASP.NET 2.0中应设置HtmlEncode为false,否则DataFormatString不起作用
例:<asp:BoundField DataField="SubTime" DataFormatString="{0:f}" HeaderText="SubTime"
            SortExpression="SubTime" HtmlEncode="False" />

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET GridView控件提供了许多内置的聚合函数,如SUM,AVG,MIN,MAX和COUNT等。这些聚合函数可以使用GridView的模板字段或数据绑定表达式来实现。 以下是一个示例,显示如何在GridView使用SUM聚合函数来计算某一列的总和: ```html <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="ProductID" HeaderText="Product ID" /> <asp:BoundField DataField="ProductName" HeaderText="Product Name" /> <asp:BoundField DataField="UnitPrice" HeaderText="Unit Price" DataFormatString="{0:C}" /> <asp:BoundField DataField="Quantity" HeaderText="Quantity" /> <asp:TemplateField HeaderText="Total" > <ItemTemplate> <%# Eval("UnitPrice") * Eval("Quantity") %> </ItemTemplate> <FooterTemplate> <%# String.Format("{0:C}", GridView1.Rows.Cast<GridViewRow>().Sum(row => Convert.ToDecimal(((Label)row.FindControl("LabelTotal")).Text)))) %> </FooterTemplate> </asp:TemplateField> </Columns> </asp:GridView> ``` 在上面的代码,我们使用一个模板字段来计算每个产品的总价,然后在GridView的页脚使用SUM聚合函数来计算所有产品的总价。在页脚模板,我们使用LINQ查询来获取GridView所有行的总和,并使用String.Format方法来格式化结果。 请注意,我们在模板字段使用了一个Label控件来显示每个产品的总价。在页脚模板,我们使用FindControl方法来获取每个行的Label控件,并将其转换为Decimal值来进行求和运算。 除了SUM聚合函数之外,您还可以使用其他聚合函数来计算平均值,最小值,最大值和计数。只需将聚合函数的名称替换为SUM即可。例如,使用AVG聚合函数来计算某一列的平均值: ```html <FooterTemplate> <%# String.Format("{0:C}", GridView1.Rows.Cast<GridViewRow>().Average(row => Convert.ToDecimal(((Label)row.FindControl("LabelTotal")).Text)))) %> </FooterTemplate> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值