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

Datagrid formatting

P: n/a
I've got what seems to be a common problem with the DataGrid control -
I want to display a dataset one of whose columns (always the first
column) is a datetime column. But only the date displays not the time.

The dataset structure is only defined at runtime so I want to set the
format of just this one datetime column programmatically. Would anyone
be kind enough to tell me the simplest way of doing so. This is just a
detail, albeit an important one, in an urgent larger project and I'd
really rather not get diverted into reading up on all the intricacies
of the DataGrid - a control I don't normally need to use!

Thanks if anyone can help.
JGD
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
John,

I use a class with 1 shared method named Format to format a datagrid at
runtime with data from a datatable. The Format method formats any datetime
columns to display the date and time. You might be able to modify the code
for your needs.

Note that the code is compiled with Option Strict OFF, so you might need to
do some type casting if you compile with Option Strict ON.

Here is the code:

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

Call the Format function like this:

GridFormatter.Format (myDataGrid, myDataTable)

Kerry Moorman

"John Dann" wrote:
I've got what seems to be a common problem with the DataGrid control -
I want to display a dataset one of whose columns (always the first
column) is a datetime column. But only the date displays not the time.

The dataset structure is only defined at runtime so I want to set the
format of just this one datetime column programmatically. Would anyone
be kind enough to tell me the simplest way of doing so. This is just a
detail, albeit an important one, in an urgent larger project and I'd
really rather not get diverted into reading up on all the intricacies
of the DataGrid - a control I don't normally need to use!

Thanks if anyone can help.
JGD

Nov 21 '05 #2

P: n/a
Johnn,

Have a look at this page
http://msdn.microsoft.com/library/de...ormattopic.asp

I hope this helps,

Cor
Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.