Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
VB
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
Posted

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.
 
Share this answer
 
So by doing this it should work? ...
VB
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 :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900