Link to home
Start Free TrialLog in
Avatar of brokeMyLegBiking
brokeMyLegBiking

asked on

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

Avatar of amyhxu
amyhxu

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.

Avatar of brokeMyLegBiking

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of amyhxu
amyhxu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial