DateTime display in a datagrid?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
Andrew S. Giles said:
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.
 
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 said:
Andrew S. Giles said:
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.
 
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 said:
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 said:
message
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.
 

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