在DataGridView列中仅显示日期 [英] Show only Date in DataGridView Column
问题描述
我有一个字段作为DateTime,我想在我的datagridview c#中只显示日期(没有时间)。我得到datetime。例如:22.03.2016 00:00:00
任何帮助将不胜感激。
我尝试了以下内容:
I have a field as DateTime, I want to display only date( without time ) in my datagridview c#. I get the datetime. example: 22.03.2016 00:00:00 Any help would be appreciated. I have tried the following:
private void bindListToGridView1(List<Item> list)
{
try
{
dataGridView1.AllowUserToAddRows = false;
ListtoDataTableConverter converter = new ListtoDataTableConverter();
DataTable dt2 = converter.ToDataTable(listToPresent);
dataGridView1.DataSource = null;
using (dt2)
{
dataGridView1.AutoGenerateColumns = false;
dataGridView1.ColumnCount = 3;
dataGridView1.Columns[0].Name = "id";
dataGridView1.Columns[0].HeaderText = "ID";
dataGridView1.Columns[0].DataPropertyName = "id";
dataGridView1.Columns[1].Name = "itemName";
dataGridView1.Columns[1].HeaderText = "Item Name";
dataGridView1.Columns[1].DataPropertyName = "itemName";
dataGridView1.Columns[2].Name = "productionDate";
dataGridView1.Columns[2].HeaderText = "Production Date";
dataGridView1.Columns[2].DataPropertyName = "productionDate";
dataGridView1.Columns[2].DefaultCellStyle.Format = "yyyy'/'MM'/'dd";
dataGridView1.DataSource = dt2;
}
}
catch (Exception ex)
{
}
}
编辑
这是我用来创建 DataTable
:
public class ListtoDataTableConverter
{
public DataTable ToDataTable<T>(List<T> items)
{
DataTable dataTable = new DataTable(typeof(T).Name);
//Get all the properties
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
//Setting column names as Property names
dataTable.Columns.Add(prop.Name);
}
foreach (T item in items)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
//inserting property values to datatable rows
values[i] = Props[i].GetValue(item, null);
}
dataTable.Rows.Add(values);
}
//put a breakpoint here and check datatable
return dataTable;
}
}
推荐答案
问题是您的列不是类型 DateTime
,所以当您应用格式化时,它不起作用。
The problem is your column is not of type DateTime
so when you apply formatting, it doesn't work.
查看创建数据表的方法,您使用 dataTable.Columns.Add(prop.Name);
创建列,这意味着列的DataType
是 string
,格式化将不适用于列。 (我以前猜到)
Take a look at the method that creates the data table, you created the columns using dataTable.Columns.Add(prop.Name);
which it means the DataType
of column is string
and formatting will not apply on column. (As I guessed previously)
注意
- 如果要在
DataGridView
中显示它,您不需要将列表转换为DataTable
,足够了将其设置为网格的DataSource
。 - 格式化
DateTime
列仅显示Date
部分,只需将该列的DefaultCellStyle.Format
设置为yyyy / MM / dd
。 - 作为快速修复方法,您可以替换这行代码
dataTable.Columns.Add (prop.Name);
与这行代码:dataTable.Columns.Add(prop.Name,prop.PropertyType);
它使用数据属性类型作为列的数据类型。
- You don't need to convert a List to a
DataTable
if you want to show it inDataGridView
, it's enough to set it asDataSource
of the grid. - To format a
DateTime
column to show onlyDate
part, it's enough to setDefaultCellStyle.Format
of that column toyyyy/MM/dd
. - As a quick fix in your method, you can replace this line of code
dataTable.Columns.Add(prop.Name);
with this line of code:dataTable.Columns.Add(prop.Name, prop.PropertyType);
which uses data type of property as data type of column.
这篇关于在DataGridView列中仅显示日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!