如何在datagridview列中更改日期格式,其中包含datetimepicker [英] How to change date format in datagridview column which has datetimepicker in it

查看:115
本文介绍了如何在datagridview列中更改日期格式,其中包含datetimepicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在vb.net windows应用程序中有一个datagridview。



我有datetimepicker在其中一列中。 (由微软提供)



如何将日期格式更改为dd / MM / yyyy(默认为MM / dd / yyyy)



通常要更改我们的格式如下所示

datagridview1.Columns(0).DefaultCellStyle.Format =dd / MM / yyyy我已经完成了





但是由于我在datagrid列中有datetimepicker,它似乎没有应用风格。



我们非常感谢您的早期帮助。



干杯

Anand

Hi,

I have a datagridview in vb.net windows application.

I have datetimepicker in one of the column. (Courtesy Microsoft)

How do I change the date format to dd/MM/yyyy ( Default is MM/dd/yyyy)

Normally to change the format we do as below
datagridview1.Columns(0).DefaultCellStyle.Format = "dd/MM/yyyy" which I have done


But as I have datetimepicker in the datagrid column it seems it is not applying the style.

An early help is highly appreciated.

Cheers
Anand

推荐答案

DateTimePickers也可以定义日期格式。不确定您使用的是哪一个,但它应该具有一个属性来指定日期格式。根据需要查找并设置。



对于MS datepicker: MSDN:DateTimePicker.CustomFormat属性 [ ^ ]

参考: MSDN:DateTimePicker类 [ ^ ]
DateTimePickers too can have date formats defined. Not sure which one you are using but it should have one of the property to specify the date format. Look for it and set as per need.

For MS datepicker: MSDN: DateTimePicker.CustomFormat Property [^]
Refer: MSDN: DateTimePicker Class[^]


嗨Anand,



对于Datetimepicker日期格式。参考

http://stackoverflow.com/questions/10589877/format -datetimepicker-in-datagridview-column



http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/bb5ef524-b2ba-4420-82ac-230d156695aa [ ^ ]



对于gridview日期列dateformat

他们有两种方法可以做到这一点



1你可以在页面后面的代码上写一个函数,比如

公共函数ReturnOnlyDate(TDate As String)As String

试试

如果TDate.Length = 19那么'12/30/1899 00:00:13

返回(TDate.Remove(10,9) )

结束如果

Catch ex As Exception

不返回

结束尝试

什么都不返回

结束函数

你应该将gridview中的日期字段转换为Template字段。并在源页面中更改这样的绑定字段





< asp:templatefield headertext =Datexmlns:asp = #unknown>



< asp:label id =Label2runat =servertext =<%#ReturnOnlyDate(Eval(TDate ))%>>





< asp:label id =Label2runat =servertext = <%#ReturnOnlyDate(Eval(TDate)))%>>









2.您可以直接将字段转换为模板字段,在源页面中可以这样做。



< ; asp:templatefield headertext =Datexmlns:asp =#unknown>



< asp:label id =Label2runat =server text =<%#Bind(TDate,{0:MM / dd / yyyy})%>>





< asp:label id =Label2runat =servertext =<%#Bind(TDate,{0:MM / dd / yyyy})%>> ;









这可能有所帮助。
Hi Anand,

For Datetimepicker date format. refer
http://stackoverflow.com/questions/10589877/format-datetimepicker-in-datagridview-column

http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/bb5ef524-b2ba-4420-82ac-230d156695aa[^]

For gridview date column dateformat
Their are two ways you can do that

1 You can write a Function on the code behind page like
Public Function ReturnOnlyDate(TDate As String) As String
Try
If TDate.Length = 19 Then ' 12/30/1899 00:00:13
Return (TDate.Remove(10, 9))
End If
Catch ex As Exception
Return Nothing
End Try
Return Nothing
End Function
and you should convert the date field in the gridview to Template field. and in the source page you change the bound field like this


<asp:templatefield headertext="Date" xmlns:asp="#unknown">

<asp:label id="Label2" runat="server" text="<%# ReturnOnlyDate(Eval("TDate"))%>">


<asp:label id="Label2" runat="server" text="<%# ReturnOnlyDate(Eval("TDate")))%>">




2. You can DIrectly convert the field to Template field and in the source page your can do like this.

<asp:templatefield headertext="Date" xmlns:asp="#unknown">

<asp:label id="Label2" runat="server" text="<%# Bind("TDate", "{0:MM/dd/yyyy}") %>">


<asp:label id="Label2" runat="server" text="<%# Bind("TDate", "{0:MM/dd/yyyy}") %>">




This could help.


这可以帮助



Dim xRowCount As Integer = 0

Do xRowCount = xDataGrid.RowCount

xDataGrid.Rows(xRowCount).Cells(2).Style.Format = Format(dd-MMM-yyyy)

xRowCount = xRowCount + 1

循环
This can help

Dim xRowCount As Integer = 0
Do Until xRowCount = xDataGrid.RowCount
xDataGrid.Rows(xRowCount).Cells(2).Style.Format = Format("dd-MMM-yyyy")
xRowCount = xRowCount + 1
Loop


这篇关于如何在datagridview列中更改日期格式,其中包含datetimepicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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