0

I have this code showing the data in my datagridview but I want to change the format of date and time how do I change the format of DateTime?

Datagrid.Columns.Add("datetime",typeof(DateTime));
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
usalh
  • 3
  • 1

2 Answers2

2

Yes, you can specify the format string for that particular column:

yourDatagrid.Columns["datetime"].DefaultCellStyle.Format = "MM/dd/yyyy";

Please note: I'm not sure about wther Columns["datetime"] is correct or not. if it gives error/not accessible means use the column index instead for column name yourDatagrid.Columns[4].DefaultCellStyle.Format = "MM/dd/yyyy";

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • Regarding your note: You can check in the .net documentation that DataGridView.Columns is of type [DataGridViewCOlumnCollection](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridviewcolumncollection?view=netframework-4.7.2). With that information you could go to the [.net Source of that class](https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/DataGridViewColumnCollection.cs,214) and you will notice that the collection can be acccesed by `columnName`. – Cleptus Sep 27 '19 at 06:28
0

You can change according to your need by doing this.

YourDataGrid.Columns["Column Name"].DefaultCellStyle.Format  = "dd/MM/yyyy HH:mm:ss";