DateTimePicker日期格式 [英] DateTimePicker Date Formatting

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

问题描述

我正在尝试将我的datetimepicker格式化为MM / dd / yyyy,但没有时间参数。

我将自定义格式设置为MM / dd / yyyy并将格式设置为自定义属性Explorerdw,但是当它将格式写入datagridview时,它是MM / dd / yyyy hh:mm:ss tt。

我的变量声明如下......可以.TEXT是问题吗?

I am trying to format my datetimepicker to MM/dd/yyyy only with no time of day parameter.
I set the custom format to MM/dd/yyyy and set format to custom in the Properties Explorerdw, but when it writes the format to a datagridview, it's "MM/dd/yyyy hh:mm:ss tt".
I have my variable declared as follows... Could the .TEXT be the issue?

Dim datepicked As Date
datepicked = DateTimePicker1.Text
'add column with datepicked
DataGridView1.Columns.Add(AddCol4)
For Each dgvr As DataGridViewRow In DataGridView1.Rows
    dgvr.Cells("Date").Value = datepicked
Next

推荐答案

尽管他们没有显示时间,DateTimePicker总是选择一个DateTime,而DateTime 总是包含一个时间。



所以datepicked,是一个DateTime无论你做什么,总会有一个时间元素。如果您希望它只在列中显示为日期,则应将DefaultCellStyle.Format设置为日期/时间格式字符串MM / dd / yyyy。



http://msdn.microsoft.com/en-us /library/vstudio/f9x2790s(v=vs.100).aspx [ ^ ]有关设置单元格格式的更多信息。
Even though they don't show the time, DateTimePicker always picks a DateTime, and DateTime always includes a time.

So datepicked, being a DateTime will always have a time element, no matter what you do. If you want it to only display as a date in your column you should set the DefaultCellStyle.Format to your Date/Time format string "MM/dd/yyyy".

See http://msdn.microsoft.com/en-us/library/vstudio/f9x2790s(v=vs.100).aspx[^] for more information on setting your cell format.


这样做应该有效吗? ...

So by doing this it should work? ...
Dim datepicked As Date
datepicked = DateTimePicker1.Text

Dim AddCol4 As New DataGridViewTextBoxColumn
AddCol4.DataPropertyName = "Date"
AddCol4.HeaderText = "Date"
AddCol4.Name = "Date"
AddCol4.DefaultCellStyle.Format = "MM/dd/yyyy" 'Adding this

DataGridView1.Columns.Add(AddCol4)
For Each dgvr As DataGridViewRow In DataGridView1.Rows
    dgvr.Cells("Date").Value = datepicked
Next





确实如此。

谢谢你,你是一位绅士和学者:)



And it does.
Thanks, you are a gentleman and a a scholar :)


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

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