PC Review


Reply
Thread Tools Rate Thread

Datagrid formatting

 
 
John Dann
Guest
Posts: n/a
 
      15th Mar 2005
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
 
Reply With Quote
 
 
 
 
=?Utf-8?B?S2VycnkgTW9vcm1hbg==?=
Guest
Posts: n/a
 
      15th Mar 2005
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
>

 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      15th Mar 2005
Johnn,

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

I hope this helps,

Cor


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Formatting DataGrid =?Utf-8?B?Sm9lIEdhaW5lcw==?= Microsoft Dot NET Framework Forms 0 11th Mar 2005 06:25 PM
datagrid formatting Hyrum Mortensen Microsoft ASP .NET 1 25th Jun 2004 10:34 PM
Datagrid Formatting MadCrazyNewbie Microsoft VB .NET 1 8th Apr 2004 12:01 PM
DataGrid Formatting Vinod I Microsoft ASP .NET 0 20th Jan 2004 02:15 PM
Datagrid formatting John Microsoft Dot NET Framework Forms 1 11th Sep 2003 07:30 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:02 AM.