C# 更改datagrid中的类型列
在我的datagrid中,我想将带有C# 更改datagrid中的类型列,c#,.net,wpf,wpfdatagrid,C#,.net,Wpf,Wpfdatagrid,在我的datagrid中,我想将带有DateTime的列类型更改为String,我该怎么做 来源是 MySqlCommand cmd = new MySqlCommand(query, conn); dt.Load(cmd.ExecuteReader()); source.DataSource = dt; dataGrid1.ItemsSource = source; 您可以为DateTime这样的值定义DataTemplate- System.Windows.Forms.BindingSo
DateTime
的列类型更改为String
,我该怎么做
来源是
MySqlCommand cmd = new MySqlCommand(query, conn);
dt.Load(cmd.ExecuteReader());
source.DataSource = dt;
dataGrid1.ItemsSource = source;
您可以为
DateTime
这样的值定义DataTemplate
-
System.Windows.Forms.BindingSource source = new System.Windows.Forms.BindingSource();
...
请参阅-为什么要将日期时间转换为字符串?是因为格式化吗?您可以通过在XAML级别将格式化选项应用于数据网格来实现这一点。
<DataGrid>
<DataGrid.Resources>
<DataTemplate DataType="{x:Type DateTime}">
<TextBlock Text="{Binding StringFormat={0:d}}" />
</DataTemplate>
</DataGrid.Resources>
...
</DataGrid>