467,900 Members | 1,376 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,900 developers. It's quick & easy.

Datagrid Time Not showing

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
  • viewed: 1768
Share:
2 Replies
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
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.

Similar topics

reply views Thread by irene | last post: by
reply views Thread by Frosty | last post: by
2 posts views Thread by enak | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.