Showing Time in Grid

  • Thread starter Sebastian Santacroce
  • Start date
S

Sebastian Santacroce

How do I get a datetime field from a database to show just
time in the datagrid. The default just shows date.

Thank you

Sebastian
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi Sebastian,

Initialize the FormatInfo property for the corresponding DataGridColumnStyle
with a pre-created DateTimeFormatInfo instance. You can probably achieve the
same effect by playing just with the DataGridColumnStyle.Format property (if
an existing time-only format exists or can be specified with a tailormade
format string).
 
S

Sebastian Santacroce

I don't know where your getting FormatInfo property from
but it doesn't exist for DataGridTableStyle object for
VB .net 2002. I've attached it to my datagrid as a
TableStyles.

The closest thing I could find in that was
DataGridTableStyle.GridColumnStyles
and it had no formating property.

Can someone advice how I get to this format property?

Thank you,

Seb

-----Original Message-----
Hi Sebastian,

Initialize the FormatInfo property for the corresponding DataGridColumnStyle
with a pre-created DateTimeFormatInfo instance. You can probably achieve the
same effect by playing just with the
DataGridColumnStyle.Format property (if
an existing time-only format exists or can be specified with a tailormade
format string).

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

How do I get a datetime field from a database to show just
time in the datagrid. The default just shows date.

Thank you

Sebastian

.
 
D

Dmitriy Lapshin [C# / .NET MVP]

Sebastian,

You just haven't dug deep enough :) The GridColumnStyles collection is just
the right direction to pursue, and the next step would get you just where
you want to be. See below.

FormatInfo is set on System.Windows.Forms.DataGridColumnStyle class. This is
actually an abstract class, and has two implementations by default -
DataGridTextBoxColumn and DataGridBoolColumn. As both of these classes
inherit from DataGridColumnStyle, they obviosuly have Format and FormatInfo
properties you can customize to achieve a desirable data format.

And the GridColumnStyles collection contains just these
DataGridTextBoxColumns and DataGridBoolColumns. Unfortunately, when the
columns are generated automatically, you cannot access the column style
instances created behind the scenes. Therefore, you will need to specify a
set of columns manually, and it is easily done with the GridColumnStyles
collection designer.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Sebastian Santacroce said:
I don't know where your getting FormatInfo property from
but it doesn't exist for DataGridTableStyle object for
VB .net 2002. I've attached it to my datagrid as a
TableStyles.

The closest thing I could find in that was
DataGridTableStyle.GridColumnStyles
and it had no formating property.

Can someone advice how I get to this format property?

Thank you,

Seb

-----Original Message-----
Hi Sebastian,

Initialize the FormatInfo property for the corresponding DataGridColumnStyle
with a pre-created DateTimeFormatInfo instance. You can probably achieve the
same effect by playing just with the
DataGridColumnStyle.Format property (if
an existing time-only format exists or can be specified with a tailormade
format string).

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

How do I get a datetime field from a database to show just
time in the datagrid. The default just shows date.

Thank you

Sebastian

.
 
S

Sebastian Santacroce

Ok. Can you give me some sample code on how to do this.
I'll attach some code I have so far so you can add the
rest to get the formatting code

GRID.TableStyles(rownum).GridColumnStyles("Date")...

Thank you,

Sebastian
-----Original Message-----
Sebastian,

You just haven't dug deep enough :) The GridColumnStyles collection is just
the right direction to pursue, and the next step would get you just where
you want to be. See below.

FormatInfo is set on
System.Windows.Forms.DataGridColumnStyle class. This is
actually an abstract class, and has two implementations by default -
DataGridTextBoxColumn and DataGridBoolColumn. As both of these classes
inherit from DataGridColumnStyle, they obviosuly have Format and FormatInfo
properties you can customize to achieve a desirable data format.

And the GridColumnStyles collection contains just these
DataGridTextBoxColumns and DataGridBoolColumns. Unfortunately, when the
columns are generated automatically, you cannot access the column style
instances created behind the scenes. Therefore, you will need to specify a
set of columns manually, and it is easily done with the GridColumnStyles
collection designer.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

I don't know where your getting FormatInfo property from
but it doesn't exist for DataGridTableStyle object for
VB .net 2002. I've attached it to my datagrid as a
TableStyles.

The closest thing I could find in that was
DataGridTableStyle.GridColumnStyles
and it had no formating property.

Can someone advice how I get to this format property?

Thank you,

Seb

-----Original Message-----
Hi Sebastian,

Initialize the FormatInfo property for the
corresponding
DataGridColumnStyle
with a pre-created DateTimeFormatInfo instance. You can probably achieve the
same effect by playing just with the
DataGridColumnStyle.Format property (if
an existing time-only format exists or can be specified with a tailormade
format string).

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Sebastian Santacroce" <[email protected]> wrote
in
message
How do I get a datetime field from a database to show just
time in the datagrid. The default just shows date.

Thank you

Sebastian

.

.
 
D

Dmitriy Lapshin [C# / .NET MVP]

GRID.TableStyles(rownum).GridColumnStyles("Date")...

Here's an example from the "DataGridTableStyle Class" topic in MSDN:

Private Sub AddCustomDataTableStyle()
' Create a new DataGridTableStyle and set
' its MappingName to the TableName of a DataTable.
Dim ts1 As New DataGridTableStyle()
ts1.MappingName = "Customers"

' Add a GridColumnStyle and set its MappingName
' to the name of a DataColumn in the DataTable.
' Set the HeaderText and Width properties.

Dim boolCol As New DataGridBoolColumn()
boolCol.MappingName = "Current"
boolCol.HeaderText = "IsCurrent Customer"
boolCol.Width = 150
ts1.GridColumnStyles.Add(boolCol)

' Add a second column style.
Dim TextCol As New DataGridTextBoxColumn()
TextCol.MappingName = "custName"
TextCol.HeaderText = "Customer Name"
TextCol.Width = 250
ts1.GridColumnStyles.Add(TextCol)
End Sub


To set up a desired format, add the following line before the
"ts1.GridColumnStyles.Add(TextCol)" line:

TextCol.Format = "t"

You can also use "T" for the long time pattern.

Hope this helps.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Sebastian Santacroce said:
Ok. Can you give me some sample code on how to do this.
I'll attach some code I have so far so you can add the
rest to get the formatting code

GRID.TableStyles(rownum).GridColumnStyles("Date")...

Thank you,

Sebastian
-----Original Message-----
Sebastian,

You just haven't dug deep enough :) The GridColumnStyles collection is just
the right direction to pursue, and the next step would get you just where
you want to be. See below.

FormatInfo is set on
System.Windows.Forms.DataGridColumnStyle class. This is
actually an abstract class, and has two implementations by default -
DataGridTextBoxColumn and DataGridBoolColumn. As both of these classes
inherit from DataGridColumnStyle, they obviosuly have Format and FormatInfo
properties you can customize to achieve a desirable data format.

And the GridColumnStyles collection contains just these
DataGridTextBoxColumns and DataGridBoolColumns. Unfortunately, when the
columns are generated automatically, you cannot access the column style
instances created behind the scenes. Therefore, you will need to specify a
set of columns manually, and it is easily done with the GridColumnStyles
collection designer.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

I don't know where your getting FormatInfo property from
but it doesn't exist for DataGridTableStyle object for
VB .net 2002. I've attached it to my datagrid as a
TableStyles.

The closest thing I could find in that was
DataGridTableStyle.GridColumnStyles
and it had no formating property.

Can someone advice how I get to this format property?

Thank you,

Seb


-----Original Message-----
Hi Sebastian,

Initialize the FormatInfo property for the corresponding
DataGridColumnStyle
with a pre-created DateTimeFormatInfo instance. You can
probably achieve the
same effect by playing just with the
DataGridColumnStyle.Format property (if
an existing time-only format exists or can be specified
with a tailormade
format string).

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

message
How do I get a datetime field from a database to show
just
time in the datagrid. The default just shows date.

Thank you

Sebastian

.

.
 
D

Dmitriy Lapshin [C# / .NET MVP]

Sebastian,

When a grid is bound to a data source, two options are actually possible:

a) All grid column styles and a default table style are created
automatically behind the scenes. In this case, you CANNOT access the column
styles created automatically for you. I know - this is sad, but true.

b) You create the table style and populate the grid column styles manually.
You can do it either completely in code as it has been done in the example,
or use the appropriate collection designers as I have said searlier in this
thread.

Thus, I'm afraid you have nothing to modify unless you have already created
a bunch of data grid column styles for your actual data columns - but you
would know what to do then. So, the bottom line is that you should take the
example given and rework it to make it work with the data returned by the
stored procedure.

IMPORTANT: You should create table styles and grid column styles BEFORE
binding the grid to the data. This might actually be the problem you've
experienced.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Sebastian Santacroce said:
Hey thanks for you help,
I think your on the right track but I'm getting the data
from a stored procedure in SQL Server and using a data
adapter and it already has a column with that name. So I
just want to modify that column not add a new one.If add
like it says below it tells me there already exists a
column mapping with that name.

What do I need to change to modify not add.

Thank you,

Sebastian
-----Original Message-----
GRID.TableStyles(rownum).GridColumnStyles("Date")...

Here's an example from the "DataGridTableStyle Class" topic in MSDN:

Private Sub AddCustomDataTableStyle()
' Create a new DataGridTableStyle and set
' its MappingName to the TableName of a DataTable.
Dim ts1 As New DataGridTableStyle()
ts1.MappingName = "Customers"

' Add a GridColumnStyle and set its MappingName
' to the name of a DataColumn in the DataTable.
' Set the HeaderText and Width properties.

Dim boolCol As New DataGridBoolColumn()
boolCol.MappingName = "Current"
boolCol.HeaderText = "IsCurrent Customer"
boolCol.Width = 150
ts1.GridColumnStyles.Add(boolCol)

' Add a second column style.
Dim TextCol As New DataGridTextBoxColumn()
TextCol.MappingName = "custName"
TextCol.HeaderText = "Customer Name"
TextCol.Width = 250
ts1.GridColumnStyles.Add(TextCol)
End Sub


To set up a desired format, add the following line before the
"ts1.GridColumnStyles.Add(TextCol)" line:

TextCol.Format = "t"

You can also use "T" for the long time pattern.

Hope this helps.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Ok. Can you give me some sample code on how to do this.
I'll attach some code I have so far so you can add the
rest to get the formatting code

GRID.TableStyles(rownum).GridColumnStyles("Date")...

Thank you,

Sebastian
-----Original Message-----
Sebastian,

You just haven't dug deep enough :) The GridColumnStyles
collection is just
the right direction to pursue, and the next step would
get you just where
you want to be. See below.

FormatInfo is set on
System.Windows.Forms.DataGridColumnStyle class. This is
actually an abstract class, and has two implementations
by default -
DataGridTextBoxColumn and DataGridBoolColumn. As both of
these classes
inherit from DataGridColumnStyle, they obviosuly have
Format and FormatInfo
properties you can customize to achieve a desirable data
format.

And the GridColumnStyles collection contains just these
DataGridTextBoxColumns and DataGridBoolColumns.
Unfortunately, when the
columns are generated automatically, you cannot access
the column style
instances created behind the scenes. Therefore, you will
need to specify a
set of columns manually, and it is easily done with the
GridColumnStyles
collection designer.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

message
I don't know where your getting FormatInfo property from
but it doesn't exist for DataGridTableStyle object for
VB .net 2002. I've attached it to my datagrid as a
TableStyles.

The closest thing I could find in that was
DataGridTableStyle.GridColumnStyles
and it had no formating property.

Can someone advice how I get to this format property?

Thank you,

Seb


-----Original Message-----
Hi Sebastian,

Initialize the FormatInfo property for the
corresponding
DataGridColumnStyle
with a pre-created DateTimeFormatInfo instance. You can
probably achieve the
same effect by playing just with the
DataGridColumnStyle.Format property (if
an existing time-only format exists or can be specified
with a tailormade
format string).

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

in
message
How do I get a datetime field from a database to show
just
time in the datagrid. The default just shows date.

Thank you

Sebastian

.


.

.
 
A

Armin Zingler

Am 10.08.2010 14:46, schrieb Janice Wilson:
I was looking for the same solution. I added only 1 line of code to make this work! What a simple, effective solution. Thanks Dmitriy

Always funny to see a reply after 7 years.
 
C

Cor

And to think it is for a datagrid which is for years already not in the
toolbox anymore.
 

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

Top