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

DateTime display in a datagrid?

P: n/a
Hello,

I am importing a flat text file, and putting it into a datagrid for display
on a form.

Currently the users have their dates and times seperated. I have two
fields, therefore in the datatable feeding the datagrid control. Both are of
the DateTime Type. How do I get the time field to display only the Time, and
not the date, which is apparently the default.

As a followup, how do I get one DateTime value to display both the date and
the time in a datagrid Control?

Thanks in advance,

Andrew S. Giles
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Andrew S. Giles" <An**********@discussions.microsoft.com> wrote in message
news:89**********************************@microsof t.com...
Hello,

I am importing a flat text file, and putting it into a datagrid for
display
on a form.

Currently the users have their dates and times seperated. I have two
fields, therefore in the datatable feeding the datagrid control. Both are
of
the DateTime Type. How do I get the time field to display only the Time,
and
not the date, which is apparently the default.
Use DateTime.ToShartTimeString() or DateTime.ToLongTimeString().
As a followup, how do I get one DateTime value to display both the date
and
the time in a datagrid Control?
You'll have to create a new DateTime object combining the relevant
properties of the two existing DateTime objects. For example, let's assume
your two existing DateTime objects are called dtDate (the date) and dtTime
(the time in hours, minutes, seconds):

DateTime dtCombined = new DateTime(dtDate.Year, dtDate.Month, dtDate.Day);
dtCombined = dtCombined + new TimeSpan(dtTime.Hour, dtTime.Minute,
dtTime.Second);

In case I misunderstood your question, and you simply want to display both
date and time of one and the same DateTime object, simply use
DateTime.ToString().
Thanks in advance,

Andrew S. Giles

--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
Nov 17 '05 #2

P: n/a
Kia,

Thanks! I have the data into the DateTime controls. My problem is that
when I put the DateTime object into the datatable that is the source for the
datagrid, all that is displayed is the Date of the DateTime, not the date and
time, or just the time.

How do I get the time to be displayed after the date, if the datatype in the
datagrid is DateTime?

Andrew

"Kai Brinkmann [MSFT]" wrote:
"Andrew S. Giles" <An**********@discussions.microsoft.com> wrote in message
news:89**********************************@microsof t.com...
Hello,

I am importing a flat text file, and putting it into a datagrid for
display
on a form.

Currently the users have their dates and times seperated. I have two
fields, therefore in the datatable feeding the datagrid control. Both are
of
the DateTime Type. How do I get the time field to display only the Time,
and
not the date, which is apparently the default.


Use DateTime.ToShartTimeString() or DateTime.ToLongTimeString().
As a followup, how do I get one DateTime value to display both the date
and
the time in a datagrid Control?


You'll have to create a new DateTime object combining the relevant
properties of the two existing DateTime objects. For example, let's assume
your two existing DateTime objects are called dtDate (the date) and dtTime
(the time in hours, minutes, seconds):

DateTime dtCombined = new DateTime(dtDate.Year, dtDate.Month, dtDate.Day);
dtCombined = dtCombined + new TimeSpan(dtTime.Hour, dtTime.Minute,
dtTime.Second);

In case I misunderstood your question, and you simply want to display both
date and time of one and the same DateTime object, simply use
DateTime.ToString().
Thanks in advance,

Andrew S. Giles

--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 17 '05 #3

P: n/a
Ah, now I understand what you're asking :) You must forgive me for being a
little dense today; my weekend wasn't as relaxing as I had hoped...

Personally, I think by far the easiest way to address this issue would be to
change the data type of the approprivate column in your DataTable from
System.DateTime to System.String. Then store DateTime.ToString() in this
column (default format is MM/DD/YYYY HH:MM:SS AM/PM; but if you don't like
that you can use a format provider). Now when you bind your DataTable to the
DataGrid you'll see the expected data.

If you need to, you can then use DateTime.Parse(System.String) to turn the
value in the DataTable back into a DateTime object for processing.
--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
"Andrew S. Giles" <An**********@discussions.microsoft.com> wrote in message
news:7D**********************************@microsof t.com...
Kia,

Thanks! I have the data into the DateTime controls. My problem is that
when I put the DateTime object into the datatable that is the source for
the
datagrid, all that is displayed is the Date of the DateTime, not the date
and
time, or just the time.

How do I get the time to be displayed after the date, if the datatype in
the
datagrid is DateTime?

Andrew

"Kai Brinkmann [MSFT]" wrote:
"Andrew S. Giles" <An**********@discussions.microsoft.com> wrote in
message
news:89**********************************@microsof t.com...
> Hello,
>
> I am importing a flat text file, and putting it into a datagrid for
> display
> on a form.
>
> Currently the users have their dates and times seperated. I have two
> fields, therefore in the datatable feeding the datagrid control. Both
> are
> of
> the DateTime Type. How do I get the time field to display only the
> Time,
> and
> not the date, which is apparently the default.


Use DateTime.ToShartTimeString() or DateTime.ToLongTimeString().
> As a followup, how do I get one DateTime value to display both the date
> and
> the time in a datagrid Control?


You'll have to create a new DateTime object combining the relevant
properties of the two existing DateTime objects. For example, let's
assume
your two existing DateTime objects are called dtDate (the date) and
dtTime
(the time in hours, minutes, seconds):

DateTime dtCombined = new DateTime(dtDate.Year, dtDate.Month,
dtDate.Day);
dtCombined = dtCombined + new TimeSpan(dtTime.Hour, dtTime.Minute,
dtTime.Second);

In case I misunderstood your question, and you simply want to display
both
date and time of one and the same DateTime object, simply use
DateTime.ToString().
> Thanks in advance,
>
> Andrew S. Giles

--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no
rights.

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.