Home >Backend Development >C++ >How to Customize Date Formatting in a DataGridView?

How to Customize Date Formatting in a DataGridView?

Linda Hamilton
Linda HamiltonOriginal
2025-01-11 14:33:461005browse

How to Customize Date Formatting in a DataGridView?

DataGridView date format customization

You want to display a "Last Action" column with a custom date format in the DataGridView. By default, Windows Forms formats DateTime values ​​according to system settings. However, you can override these settings programmatically using the DefaultCellStyle property.

For a specific column, you can control its date format as follows:

<code class="language-csharp">dataGrid.Columns[2].DefaultCellStyle.Format = "MM/dd/yyyy HH:mm:ss";</code>

This line of code adjusts the format string to "MM/dd/yyyy HH:mm:ss" (for example, 2010/10/27 12:00:19).

If you prefer a 12-hour clock with AM/PM indicators, use the following format string instead:

<code class="language-csharp">dataGrid.Columns[2].DefaultCellStyle.Format = "MM/dd/yyyy hh:mm:ss tt";</code>

The above is the detailed content of How to Customize Date Formatting in a DataGridView?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn