Datagrid Time Not showing

  • Thread starter Thread starter TerryW
  • Start date Start date
T

TerryW

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
 
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
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top