473,657 Members | 2,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Datagrid formatting

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 programmaticall y. 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
Nov 21 '05 #1
2 1457
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 DataGridTableSt yle()
Dim tc As DataGridTextBox Column

Dim dc As DataColumn
For Each dc In dt.Columns
tc = New DataGridTextBox Column()
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.GridColumnSt yles.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.F ormat (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 programmaticall y. 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

Nov 21 '05 #2
Johnn,

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

I hope this helps,

Cor
Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1818
by: Frnak McKenney | last post by:
One of the reasons given for the Allied victory in WWI is that the Nazi armament industry invested so much effort in creating new weapons (e.g. the jet plane) it wasn't able to develop any of them to the point of mass-production. There are days when I experience the same difficulties with C# and dotNET: there are twelve ways to do 'most _any_thing, and I wind up exploring six before I find an acceptable solution. <grin> Case in...
7
4670
by: Matthew Wieder | last post by:
Hi - I have a datagrid that has a black header and the rows alternate white and gray. The problem is that until items are added to the grid, the grid appears as a large black rectangle, which is quite ugly. The black area is larger then it is once an item is added. How can I solve this problem either by: A) Having only the header black and the rest of the empty block white or gray (preffered solution) B) Having the color gray or white...
1
2309
by: J.B | last post by:
I have a datagrid that will display different datasets, (it runs different sprocs based on a value in the querystring) but it will always return 6 columns. The 5th and 6th columns are always Date and Currency format, all others are strings. I'd like to build one datagrid to handle this datagrid dynamically. I have it working now, all except the formatting of the 5th (Date) and 6th (Currency) columns. Since the headers for those...
2
3125
by: Sebi | last post by:
Hello all is it possible to add a checkbox in a DataGrid for Boolean Data? Thanks in advance
3
4067
by: McNutt Consulting | last post by:
I'm trying to implement a datagrid with dynamically created columns. The data is coming out of a very simple XML file, and it's bound through a dataview. The datagrid is just a shell definition with no column definitions in the ..aspx file. The grid is binding fine with all the data being displayed, but my numeric columns aren't being formatted. I'm using the "dataformattextstring", but it doesn't seem to be working. Is there...
5
2738
by: tshad | last post by:
I have been trying to figure out what the Datagrid is doing to create its formatting. I found that some of my Datagrids have a 3D type of border and sometime it has a straight line. I finally found that it comes down to whether there is a "rules=all" attribute (which gives me just a thin black line). If that attribute is not there, you get a 3D effect. I haven't yet been able to figure out what causes it. Here is the table def that...
5
4561
by: cwbp17 | last post by:
Hi all, Have a datagrid that displays the price column of a table. Went to the Datagrid's Property builder and set the 'Data Formatting expression' for the PRICE column to {0:c}, so that the dollar sign, the comma, and the decimal are included (i.e. $44,644.48) Managed to update the price column of the datagrid back to the database, as long as, the user doesn't delete the dollar sign (in the 0 position).
4
3229
by: hope | last post by:
Hi, How can I format a string field using Data Formatting Expression property in datagrid? For example: format last name from BROWN to Brown. Thanks
2
11704
by: DraguVaso | last post by:
Hi, I'm looking for a nice way to format the cell of my datagrid according to some predefined 'rules'. I found a lot on the site of George shepherd, but it wasn't really what I was looking for: I found there: How can I change the font used in a Gridcell on a cell by cell or row by row basis. Wih this code I am able to change the Font/Background/... based on the position of the field (column and row). The problem is: it only changes...
1
1696
by: Michael Calvin | last post by:
Some basic things I cannot figure out how to do with new datagrid. 1) How do I control the formatting of the column header separately from the rest of the column? 2) If I align right the column, the header text gets jammed to the right almost so you cannot read the last letter... how do I pad the cell? 3) Cell padding in general?
0
8385
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8303
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8821
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8502
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1941
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.