'C# - format DataGridView date into messagebox
I have a datagridview connected to a sql database and everything works fine. I want to show the field "Date" (datetime on the database) formatted just as "dd/MM/yyyy" on a MessageBox.
I have it like this:
MessageBox.Show(dataGridView1.CurrentRow.Cells[4].Value.ToString());
and it shows:
What can I add or edit in the code to remove the hours?
Solution 1:[1]
Fixed it:
string message = (dataGridView1.CurrentRow.Cells[4].Value.ToString());
MessageBox.Show(message.Remove(10).ToString());
What this does is remove the last characters of the string, in this case the characters being the time
Solution 2:[2]
Pass correct Date format i.e "dd/MM/yyyy"
as a parameter to ToString()
,
//As per your comment it seems type of ..Cells[4].Value is DateTime?
string dateValue = dataGridView1.CurrentRow.Cells[4].Value?.ToString("dd/MM/yyyy") ?? "No Date Found";
MessageBox.Show(dateValue);
Use correct format for Month,
mm - Prints minute (00 - 59)
MM - Prints month (00 - 12)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Prasad Telkikar |
Solution 2 |