我使用的是DataTable
(在代码中创建),我使用DataGrid
显示它是DefaultView
。该DataTable
包含DataColumn
有DateTime
因为它的DataType
。C#WPF DataGrid - DateTime格式
的DataGrid
显示为日期:10/9/2017 12:00:00 AM
。有没有不改变列的DataType
到string
显示DateTime
值10/9/2017
的方法吗?
感谢提前:)
我使用的是DataTable
(在代码中创建),我使用DataGrid
显示它是DefaultView
。该DataTable
包含DataColumn
有DateTime
因为它的DataType
。C#WPF DataGrid - DateTime格式
的DataGrid
显示为日期:10/9/2017 12:00:00 AM
。有没有不改变列的DataType
到string
显示DateTime
值10/9/2017
的方法吗?
感谢提前:)
你的绑定应该像这样
{Binding Property, StringFormat=d}"
完整的示例
<DataGridTextColumn Header="Your Header" Binding="{Binding Property, StringFormat=d}" ></DataGridTextColumn>
您可以处理为DataGrid
的AutoGeneratingColumn
事件:
private void dg_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyType == typeof(DateTime))
{
e.Column = new DataGridTextColumn()
{
Binding = new Binding(e.PropertyName) { StringFormat = "d" }
};
}
}
在XAML中DataGrid中不包含列定义,它只是绑定到DataTable的默认视图。完美的作品,除了日期显示。 – Njubster
让c。给我一些时间 – Ramankingdom