|
GridView实现自动编号: 效果图: 10.GridView实现自定义时间货币等字符串格式: 效果图:  图1-未格式化前 图2-格式化后 解决方法: 在asp.net 2.0中,如果要在绑定列中显示比如日期格式等,如果用下面的方法是显示不了的 1 <asp :BoundField DataField="CreationDate" 2 DataFormatString="{0:M-dd-yyyy}" 3 HeaderText="CreationDate" /> 主要是由于htmlencode属性默认设置为true,已防止XSS攻击,安全起见而用的,所以,可以有以下两种方法解决 1、 1 <asp:GridView ID="GridView1" runat="server"> 2 <columns> 3 <asp :BoundField DataField="CreationDate" 4 DataFormatString="{0:M-dd-yyyy}" 5 HtmlEncode="false" 6 HeaderText="CreationDate" /> 7 </columns> 8 </asp:GridView> 将htmlencode设置为false即可 另外的解决方法为,使用模版列 1 <asp:GridView ID="GridView3" runat="server" > 2 <columns> 3 <asp:TemplateField HeaderText="CreationDate" > 4 <itemtemplate> 5 <asp:Label ID="Label1" runat="server" Text='<%# Bind("CreationDate", "{0:M-dd-yyyy}") %>' /> 6 </itemtemplate> 7 <edititemtemplate> 8 <asp:Label ID="Label1" runat="server" Text='<%# Eval("CreationDate", "{0:M-dd-yyyy}") %>' /> 9 </edititemtemplate> 10 </asp:TemplateField> 11 </columns> 12 </asp:GridView> 附录-常用格式化公式: {0:C} 货币; {0:D4}由0填充的4个字符宽的字段中显示整数; {0:000.0}四舍五入小数点保留第几位有效数字; {0:N2}小数点保留2位有效数字;{0:N2}% 小数点保留2位有效数字加百分号; {0:D}长日期;{0:d}短日期;{0:yy-MM-dd} 例如07-3-25;;{0:yyyy-MM-dd} 例如2007-3-25 |