Remove time from Date column, Datagrid

gate7cy

Well-known member
Joined
May 11, 2009
Messages
119
Programming Experience
3-5
In my application I have several datagrids databinded to various tables. Within these tables I use many times a date field. The database is made in access. At the beginning I had the date format on default as long date. When I saw that time was added I changed it to shortdate. Still I get the time in the various datagrids. How do I remove the time from the datagrid so no time is entered when I create a new field. Thanks for the replies
 
You should post DataGrid questions in the ASP.NET section.

Set the DataFormatString property for the bound column to {0:d} for short date format.
 
Kind of a newbie in vb.net. Please be patient with my questions. My application is in vb.net and the datagrids are in winforms. So why should I post it in asp.net. Is it the same in winforms (vb.net) as in asp.net? Setting the property as you said, how do I set it? I already done a search online for solutions and I found the same answer elsewhere but could not set this property. Thank you for the replies
 
Try the DataGridView control instead, here you can select among various DateTime display formats in the columns DefaultCellStyle.
 
Oops I had that as GridView in my head somehow, sorry. Use JohnH's suggestion with the DataGridView.

To set it open the DataGridView Tasks (little arrow in upper right corner)
Click Edit Columns
Select your column and click on the Defaul Cell Style property
Set the Format property to 'd'
 
Thanks all for the replies. This worked just fine.I am trying to refresh the datagrid using parameter queries. I am filtering the grid using the date column. But I always get an empty grid. Here is the code.

VB.NET:
    Private Sub DateTimePicker1_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
        Dim dd As DateTime = CDate(CStr(Me.DateTimePicker1.Value))
        Me.TextBox1.Text = dd.ToString("mm/dd/yyyy")
        dd = CDate(Me.TextBox1.Text)


        Me.ServicesTableAdapter.testdate(Me.Database2DataSet.Services, dd)



    End Sub

thanks for the replies
 
mm/dd/yyyy should be MM/dd/yyyy

What happens when you try this?:

VB.NET:
Me.ServicesTableAdapter.testdate(Me.Database2DataSet.Services, CDate(Me.DateTimePicker1.Value.ToString("MM/dd/yyyy"))
 
Why would you convert DTP.Value anyway? It already returns a Date value, the return type is DateTime which is a Date.

Note that time is always a part of the Date value, if you want to reset the time part to 0 (midnight) you can use the Date property; DTP.Value.Date.

Since you query items with a single Date parameter you also have to make sure the Date values in your database is normalized to time 0 in a similar fashion, else you will find no records because the time part of the Date value would differ. If this is not possible you have to query a Date range that includes any time of that day.
 
Strict On forces me to change it. How do I normalise my time in the database to midnight for all entries? How can I do it for the allready included entries and how will I do it in vb for the new entries. Thanks for the help. You are helping alot guys. Sorry though for the many questions
 
i have undo all the conversions and now i have this code

VB.NET:
Me.ServicesTableAdapter.testdate(Me.Database2DataSet.Services, (Me.DateTimePicker1.Value))

I still get an empty datagridview
 
remove the time from the 'Value' property of all my datetimepickers so now no time is inserted and the queries work fine. Thanks for all your replies
 
Back
Top