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?
dagpcsAsked:
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)

        '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.


dagpcsAuthor Commented:
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?
amyhxuCommented:
For displaying the date in textbox, use: textbox1.Text = Format(theDate, "MM/dd/yyyy").
Since it is editable, you can use TextBox.Leave event or TextBox.LostFocus event, in that event, you can do some validation:

   Dim dt as Date
   Try
        dt = Date.Parse(TextBox1.Text)
    Catch ex as Exception
        MessageBox.Show("Incorrect date format!")
        TextBox1.Focus()
        Exit Sub
    End Try

    TextBox1.Text = Format(dt, "MM/dd/yyyy")
   

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.