简体   繁体   English

如何在datagridview中为特定列设置自定义日期格式? - C#

[英]how to set custom date format for specific column in datagridview? - c#

I am having many fields in a table. 我在桌子上有很多字段。

Each field contains different data types ie text, number, date/time. 每个字段包含不同的数据类型,即文本,数字,日期/时间。

I am displaying the table in datagridview. 我在datagridview中显示表。

Default date format is M/dd/yyyy. 默认日期格式为M / dd / yyyy。

But i want the date to display in a custom format ie dd/M/yyyy in a datagridview. 但我希望日期以自定义格式显示,即数据网格视图中的dd / M / yyyy。

I tried changing the format in default cell style as dd/M/yyyy. 我尝试将默认单元格样式的格式更改为dd / M / yyyy。 Now the date column works fine but the number columns are displaying as dd/M/yyyy. 现在日期列工作正常但数字列显示为dd / M / yyyy。

string connetionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=database1.mdb;";
// select * from ~tablename~; 
string sql = "select * from table1";
OleDbConnection connection = new OleDbConnection(connetionString);
OleDbDataAdapter dataadapter = new OleDbDataAdapter(sql, connection);
DataSet ds = new DataSet();
connection.Open();
dataadapter.Fill(ds, vehicleno);
dataGridView1.DataSource = ds.Tables[0];
connection.Close();

How do I set custom date format for specific column in datagridview? 如何在datagridview中为特定列设置自定义日期格式?

Try this line after binding your data source: 绑定数据源后尝试以下行:

this.dataGridView1.Columns["YourDateCol"].DefaultCellStyle.Format = "dd/MM/yyyy";

Where "YourDateCol" is the name of your date column in the database. 其中“YourDateCol”是数据库中日期列的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM