61

I'm using a DataGridView with object data binding to display information about logging entities in a system, retrieved via SOAP from a remote service. One of the columns is called "Last action" and means the last time the entity logged a message. It is a System.DateTime value. When I read the SOAP response (example below), he timestamp obviously contains all the information, up to second fractions.

<LoggingEntity>
<host>marty86ce</host>
<process>10148</process>
<logger>http_core</logger>
<appName>httpd</appName>
<ffda>true</ffda>
<lastAction>2010-10-27T12:00:19.5117509Z</lastAction>
<lastHeartbeat>0001-01-01T00:00:00</lastHeartbeat>
<channelId>em_9BA2A2B4D0B6E66</channelId>
<ffdaChannelId>em_E7C8D1D4DE8EEB9</ffdaChannelId>
</LoggingEntity>

When I display it on the table, I instead can read up to the minutes https://i.stack.imgur.com/dLYBz.png I use the following code to do the data binding when I press the refresh button

public void RefreshEntities()
{
    IEntityManagement management = EntityPlugin.GetProxy();
    LoggingEntity[] result = management.FindLoggingEntities(new TemplateQuery { ffdaSpecified = true, ffda = true }); //Remote invocation

    Invoke(new MethodInvoker(delegate { gridEntities.DataSource = result; })); //THIS does the data binding from the array

    Invoke(new MethodInvoker(delegate { btnRefresh.Enabled = true; }));
}

I would like to know how to control column formatting of data bound values. I think that the format dd/MM/yyyy hh:mm comes from my system's settings. How to override formatting settings programmatically?

Thank you in advance

Full solution code on Subversion (FFDAGui program) for the Logbus-ng open source project.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
usr-local-ΕΨΗΕΛΩΝ
  • 26,101
  • 30
  • 154
  • 305

7 Answers7

58

If it is a windows form Datagrid, you could use the below code to format the datetime for a column

dataGrid.Columns[2].DefaultCellStyle.Format = "MM/dd/yyyy HH:mm:ss";

EDIT :

Apart from this, if you need the datetime in AM/PM format, you could use the below code

dataGrid.Columns[2].DefaultCellStyle.Format = "MM/dd/yyyy hh:mm:ss tt";
Sarath Subramanian
  • 20,027
  • 11
  • 82
  • 86
  • 1
    What if you don't know which column may or may not contain a datetime? Like a dynamic query tool. How would you dynamically match a date entry and change how it gets formatted? – Trevor Vance Jan 19 '23 at 21:50
  • 1
    Format does not show up for DefaultCellStyle in intellisense. – Doug Kimzey Jun 13 '23 at 18:23
45

Use Column.DefaultCellStyle.Format property or set it in designer

Denis Palnitsky
  • 18,267
  • 14
  • 46
  • 55
30

You can set the format you want:

dataGridViewCellStyle.Format = "dd/MM/yyyy";
this.date.DefaultCellStyle = dataGridViewCellStyle;
// date being a System.Windows.Forms.DataGridViewTextBoxColumn
Jla
  • 11,304
  • 14
  • 61
  • 84
  • 5
    `dd/MM/yyyy` formats the date as `dd-MM-yyyy`. Use `dd'/'MM'/'yyyy` to format the date as `dd/MM/yyyy` – TFischer May 16 '14 at 17:52
  • 3
    `yourColumn.DefaultCellStyle = new DataGridViewCellStyle { Format = "dd'/'MM'/'yyyy hh:mm:ss" };` – Appulus May 05 '15 at 22:33
  • Unfortunately it isnt changing anything for me. My date stays as it is. Looks like the date in my column is treated as a string which makes it non-formatable. Are there special requirements to the dates apart from the point, that they are real date-values? – C4d Feb 05 '16 at 11:18
  • Even for me it does not work, I tried all the ways mentioned here. I'm using BindingSource! – Lorenzo Belfanti May 05 '16 at 08:09
  • im sorry, what is your `dataGridViewCellStyle` ? – Serenade Jun 16 '17 at 07:48
2

I used these code Hope it could help

dataGridView2.Rows[n].Cells[3].Value = item[2].ToString();
dataGridView2.Rows[n].Cells[3].Value = Convert.ToDateTime(item[2].ToString()).ToString("d");
  • Could you add some explanation? – Moon Cheesez Jun 05 '16 at 13:06
  • This is if you are manually populating the rows, and it is desirable for you to convert the DateTime to a String. The ToString("d") specifies the date only format from the DateTime ToString method. – galamdring Jun 02 '17 at 13:59
1

You can set the format in aspx, just add the property "DateFormatString" in your BoundField.

DataFormatString="{0:dd/MM/yyyy hh:mm:ss}"
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
1

Published by Microsoft in Standard Date and Time Format Strings:

dataGrid.Columns[2].DefaultCellStyle.Format = "d"; // Short date

That should format the date according to the person's location settings.

This is part of Microsoft's larger collection of Formatting Types in .NET.

0
string stringtodate = ((DateTime)row.Cells[4].Value).ToString("MM-dd-yyyy");
textBox9.Text = stringtodate;
csabinho
  • 1,579
  • 1
  • 18
  • 28
user10962728
  • 15
  • 1
  • 7