change datagrid column display format


I am displaying data using a datagrid. However, when I display a datetime field from MS SQLserver, it only displays the date, and not the time.

Is there a way to change the format of a column in a datagrid so that both the date and the time are displayed?

-brokeMyLegBiking

LVL 4
brokeMyLegBikingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

amyhxuCommented:
If you are not using datagrid tablestyle, after you set the datasource of the datagrid, add the following code:

        Dim myGridTableStyle As DataGridTableStyle = New DataGridTableStyle
        myGridTableStyle.MappingName = DataGrid1.DataSource.TableName
        DataGrid1.TableStyles.Clear()
        DataGrid1.TableStyles.Add(myGridTableStyle)

        'ColumnNumber1 is the datetime column number
        Dim myTextBoxColumn1 As DataGridTextBoxColumn = DataGrid1.TableStyles(0).GridColumnStyles(ColumnNumber1)
        myTextBoxColumn1.Format = "yyyy/MM/dd HH:mm:ss"

If you are using table style, just add  myTextBoxColumn1.Format = "yyyy/MM/dd HH:mm:ss" to that column.

brokeMyLegBikingAuthor Commented:
that seems like a lot of lines of code just to change the format for one column.

I don't quite understand what the code is doing here. Is it reading in a table that contains styles for each column?
amyhxuCommented:
To change the format of one column, you have to use tablestyle. The first part of the code adds tablestyle(default) to the datagrid. The second part gets the column and sets the format, and you can add more code to set other properties of that column. e.g.
        myTextBoxColumn1.HeaderText = "ColumnName"
        myTextBoxColumn1.Width = 100
        myTextBoxColumn1.NullText = ""
        ...

Make the column look nicer.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.