By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,537 Members | 929 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,537 IT Pros & Developers. It's quick & easy.

Datagrid Time Not showing

P: n/a
I am using a system.windows.forms.datagrid and i set it's data
source to a system.data.datatable which has a column that's
dataType is system.dateTime. When I try to enter minutes and
seconds into the grid it removes that time data and leaves only
the date. The time information is still there though.

How can I make the time visible?

TIA TerryW

Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Terry,

When I needed to display date and time values in a data grid I looked for a
simple solution. But I could not find any straightforward way to get it to
work.

Even though I suspect that there is a simple solution, I came up with a
solution that worked for me.

I created a GridFormatter class with 1 shared method, Format. The Format
method accepts a data grid and a data table as arguments. The method formats
all datetime columns in the dataset to display as both date and time.

You may find this code useful as a starting point for what you need to do.

Public Class GridFormatter

Public Shared Sub Format(ByRef grd As DataGrid, ByRef dt As DataTable)

Dim ts As New DataGridTableStyle()
Dim tc As DataGridTextBoxColumn

Dim dc As DataColumn
For Each dc In dt.Columns
tc = New DataGridTextBoxColumn()
tc.MappingName = dc.ColumnName
tc.HeaderText = tc.MappingName

If dc.DataType Is GetType(System.DateTime) Then
tc.Format = "MM/dd/yyyy hh:mm:ss"
tc.Width = 125
End If

ts.GridColumnStyles.Add(tc)
Next

grd.TableStyles.Clear()
grd.TableStyles.Add(ts)

grd.DataSource = Nothing
grd.DataSource = dt

End Sub

End Class

Use the GridFormatter class like this:

GridFormatter.Format(myGrid, myDataTable)

Hope this helps,

Kerry Moorman
"TerryW @vbssys.com>" wrote:
I am using a system.windows.forms.datagrid and i set it's data
source to a system.data.datatable which has a column that's
dataType is system.dateTime. When I try to enter minutes and
seconds into the grid it removes that time data and leaves only
the date. The time information is still there though.

How can I make the time visible?

TIA TerryW

Nov 21 '05 #2

P: n/a
Thank you very much for your reply,

Sometimes it is hard to do the extra work when it seems like it should
be done easily.

I appreciate your post. and thanks for the example

On Thu, 18 Nov 2004 06:51:03 -0800, "Kerry Moorman"
<Ke**********@discussions.microsoft.com> wrote:
Terry,

When I needed to display date and time values in a data grid I looked for a
simple solution. But I could not find any straightforward way to get it to
work.

Even though I suspect that there is a simple solution, I came up with a
solution that worked for me.

I created a GridFormatter class with 1 shared method, Format. The Format
method accepts a data grid and a data table as arguments. The method formats
all datetime columns in the dataset to display as both date and time.

You may find this code useful as a starting point for what you need to do.

Public Class GridFormatter

Public Shared Sub Format(ByRef grd As DataGrid, ByRef dt As DataTable)

Dim ts As New DataGridTableStyle()
Dim tc As DataGridTextBoxColumn

Dim dc As DataColumn
For Each dc In dt.Columns
tc = New DataGridTextBoxColumn()
tc.MappingName = dc.ColumnName
tc.HeaderText = tc.MappingName

If dc.DataType Is GetType(System.DateTime) Then
tc.Format = "MM/dd/yyyy hh:mm:ss"
tc.Width = 125
End If

ts.GridColumnStyles.Add(tc)
Next

grd.TableStyles.Clear()
grd.TableStyles.Add(ts)

grd.DataSource = Nothing
grd.DataSource = dt

End Sub

End Class

Use the GridFormatter class like this:

GridFormatter.Format(myGrid, myDataTable)

Hope this helps,

Kerry Moorman
"TerryW @vbssys.com>" wrote:
I am using a system.windows.forms.datagrid and i set it's data
source to a system.data.datatable which has a column that's
dataType is system.dateTime. When I try to enter minutes and
seconds into the grid it removes that time data and leaves only
the date. The time information is still there though.

How can I make the time visible?

TIA TerryW


Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.