Link to home
Start Free TrialLog in
Avatar of dagpcs
dagpcs

asked on

How do you format a date as MM/DD/YYYY (without the time) when using DataBinding?

What is the best way to format the date as MM/DD/YYYY (without the time) when using DataBinding?  We have a DataTable that contains a date column which is bound to a column in a DataGrid.  Additionally, when a row of the DataGrid has focus, the contents of the Date cell is also displayed in a TextBox using the TextBox 'DataBindings Property'.  The value in the TextBox is modifiable.  What would be the best way to format the date and have it represented as desired in all 3 locations?
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)

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

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


Avatar of dagpcs

ASKER

Thank you for your prompt response.  We are using tablestyle but find that adding myTextBoxColumn1.Format = "MM/dd/yyyy" to that column formats the date correctly in the datagrid.  However, a textbox is also bound to the same datasource and there the time is still displaying.  Do you have any additional suggestions?
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