Zean Zean - 2 months ago 20
C# Question

Converting Date value from GridView and putting it in a TextBox in correct format

I am trying to take a Date value from a GridView in Windows Forms and putting it in the correct format in a text TextBox. A MySQL database has this field as Date, so it displays a weird 12:00:00 AM behind the Date, which does not show in the GridView but it does in the TextBox.

Here is the Code I am using to transport the Date from the GridView into the TextBox:

if (jobCardGridView.Rows.Count > 0)
submitedBy_tb.Text = jobCardGridView.SelectedRows[0].Cells["Submited By"].Value.ToString();
date_tb.Text = jobCardGridView.SelectedRows[0].Cells["Date"].Value.ToString();
nextService_tb.Text = jobCardGridView.SelectedRows[0].Cells["Next Service"].Value.ToString();

Say the GridView displays
23 11 2016
, the TextBox will display
23 11 2016 12:00:00 AM
, to my inconvenience, as I'm trying to display the Date in a

How can I achieve this?


If you have applied a format to the cell, you can use:

jobCardGridView.SelectedRows[0].Cells["Next Service"].FormattedValue().ToString();

If the .Value property it's a DateTime, can cast it and then format the final result:

DateTime t = (DateTime)jobCardGridView.SelectedRows[0].Cells["Next Service"].Value;
nextService_tb.Text = t.ToString("dd/MM/yy");