Therenho
Therenho

Reputation: 163

Change the datetime display format on datagridview date column

I want to change the default format(MM/dd/yyyy) of the datetime in the database table to (dd/MM/yyyy) when select and write the date with (dd/MM/yyyy) on the datagridview's date column.

Below is my code to convert string input date for instance: 24/4/2013 to datetime and then convert to 4/24/2013 using query and select records from database.

   string date = this.ToolStripTextBox2.Text;
   DateTime dtDate = DateTime.ParseExact(date, "d/M/yyyy", null);

Query: FillByDate

   SELECT Record_ID, Grade, Type, 
   CONVERT(VARCHAR(10), CAST(Date AS DATETIME), 103) AS Date 
   FROM RecordsTable
   WHERE Date >= CONVERT(datetime, @fromDate, 105) 
   AND Date <= CONVERT(datetime, @toDate, 105)
   ORDER BY Date ASC

But it give this error;

"String was not recognized as a valid DateTime.Couldn't store <24/04/2013> in Date Column. Expected type is DateTime."

I know that the CONVERT part was wrong as if I remove the convert part out, the date column can be written but in 4/24/2013 format. I have no idea how to correct it. Anyone can help? Thanks

Upvotes: 2

Views: 7371

Answers (2)

Therenho
Therenho

Reputation: 163

I found the solution on this thread. How to format DateTime columns in DataGridView?

just set the default cell style in the designer. I am sorry that i ask this kind of question before further searching. Thanks

Upvotes: 1

sreeju kp
sreeju kp

Reputation: 143

string date = this.ToolStripTextBox2.Text;
   DateTime dtDate = DateTime.ParseExact(date, "dd/MM/yyyy", null);

Try this code

Upvotes: 1

Related Questions