PC Review


Reply
Thread Tools Rate Thread

Datagrid Time Not showing

 
 
TerryW
Guest
Posts: n/a
 
      17th Nov 2004
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

 
Reply With Quote
 
 
 
 
=?Utf-8?B?S2VycnkgTW9vcm1hbg==?=
Guest
Posts: n/a
 
      18th Nov 2004
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
>
>

 
Reply With Quote
 
TerryW
Guest
Posts: n/a
 
      18th Nov 2004
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"
<(E-Mail Removed)> 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
>>
>>


 
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
Appointment times in Outlook showing current time not set time =?Utf-8?B?TXJzIEJlYXI=?= Microsoft Outlook Discussion 0 6th Jul 2006 07:56 AM
Email Receive time is showing 2h and 30min of actual time. =?Utf-8?B?Ym9vbg==?= Microsoft Outlook Discussion 0 26th Apr 2006 11:23 AM
ASP 1.1: DataGrid - Showing/Not Showing Buttons Ray Booysen Microsoft ASP .NET 2 28th Mar 2006 03:49 PM
datagrid combo box selection not showing in datagrid JL3574 l Microsoft C# .NET 0 19th Oct 2005 05:47 PM
Datagrid: Showing and Hiding and image in a datagrid cell. Linda Mason Microsoft C# .NET 0 24th Mar 2004 10:19 PM


Features
 

Advertising
 

Newsgroups
 


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