Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format DateTime columns in DataGridView?

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 http://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.

like image 331
usr-local-ΕΨΗΕΛΩΝ Avatar asked Oct 27 '10 12:10

usr-local-ΕΨΗΕΛΩΝ


People also ask

How to format DateTime in DataGridView c#?

You can try this: dataGridView1. Columns[0]. DefaultCellStyle. Format = "dd/MM/yyyy"; Hope this will help you.


2 Answers

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"; 
like image 119
Sarath KS Avatar answered Oct 16 '22 12:10

Sarath KS


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

like image 30
Denis Palnitsky Avatar answered Oct 16 '22 10:10

Denis Palnitsky