您的位置:程序门 -> .net技术 -> vb.net



.net 2003下 winform 的 datagrid  日期格式的显示问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


.net 2003下 winform 的 datagrid 日期格式的显示问题
发表于:2008-02-27 09:22:19 楼主
.net   2003下   winform   的   datagrid   怎么显示

yyyy-mm-dd   hh:mm:ss   格式的日期

用默认的显示时,只显示出   yyyy-mm-dd
发表于:2008-02-27 10:47:211楼 得分:0
参看datagridtablestyle,datagridtextboxcolumn
在datagridtextboxcolumn的属性format中设.
发表于:2008-02-27 15:37:282楼 得分:0
{0:yyyy-mm-dd}      

发表于:2008-02-27 15:39:343楼 得分:0
或{0:d}
发表于:2008-02-27 16:03:294楼 得分:0
10.gridview实现自定义时间货币等字符串格式:

效果图:
图1-未格式化前

图2-格式化后


解决方法:
在asp.net   2.0中,如果要在绑定列中显示比如日期格式等,如果用下面的方法是显示不了的

html code
<asp :boundfield datafield="creationdate" dataformatstring="{0:m-dd-yyyy}" headertext="creationdate" />


主要是由于htmlencode属性默认设置为true,已防止xss攻击,安全起见而用的,所以,可以有以下两种方法解决

1、

html code
<asp :gridview id="gridview1" runat="server"> <columns> <asp :boundfield datafield="creationdate" dataformatstring="{0:m-dd-yyyy}" htmlencode="false" headertext="creationdate" /> </columns> </asp>


将htmlencode设置为false即可

另外的解决方法为,使用模版列

html code
<asp :gridview id="gridview3" runat="server" > <columns> <asp :templatefield headertext="creationdate" > <edititemtemplate> <asp :label id="label1" runat="server" text='<%# eval("creationdate", "{0:m-dd-yyyy}") %>'> </asp> </edititemtemplate> <itemtemplate> <asp :label id="label1" runat="server" text='<%# bind("creationdate", "{0:m-dd-yyyy}") %>'> </asp> </itemtemplate> </asp> </columns> </asp>


前台代码:
<
html code
asp:gridview id="gridview1" runat="server" autogeneratecolumns="false" datakeynames="身份证号码" datasourceid="sqldatasource1" allowsorting="true" backcolor="white" bordercolor="#cccccc" borderstyle="none" borderwidth="1px" cellpadding="3" font-size="12px" onrowdatabound="gridview1_rowdatabound"> <columns> <asp:boundfield datafield="身份证号码" headertext="身份证号码" readonly="true" sortexpression="身份证号码" /> <asp:boundfield datafield="姓名" headertext="姓名" sortexpression="姓名" /> <asp:boundfield datafield="邮政编码" headertext="邮政编码" sortexpression="邮政编码" /> <asp:boundfield datafield="出生日期" headertext="出生日期" sortexpression="出生日期" /> <asp:boundfield datafield="起薪" headertext="起薪" sortexpression="起薪" /> </columns> <footerstyle backcolor="white" forecolor="#000066" /> <rowstyle forecolor="#000066" /> <selectedrowstyle backcolor="#669999" font-bold="true" forecolor="white" /> <pagerstyle backcolor="white" forecolor="#000066" horizontalalign="left" /> <headerstyle backcolor="#006699" font-bold="true" forecolor="white" /> </asp:gridview> <asp:sqldatasource id="sqldatasource1" runat="server" connectionstring="<%$ connectionstrings:北风贸易connectionstring1 %>" selectcommand="select top 5 [出生日期], [起薪], [身份证号码], [姓名], [家庭住址], [邮政编码] from [飞狐工作室]" datasourcemode="datareader"></asp:sqldatasource>


附录-常用格式化公式:
{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

发表于:2008-02-27 16:18:345楼 得分:0
format('日期',yyyy-mm-dd)
发表于:2008-02-27 16:19:246楼 得分:0
to   conan304  

你的那个好像是清清月儿的blog中的宝贝阿

hoho~~~


快速检索

最新资讯
热门点击