【问题标题】:Change type column in datagrid更改数据网格中的类型列
【发布时间】:2012-07-13 07:56:36
【问题描述】:
MySqlCommand cmd = new MySqlCommand(query, conn);
dt.Load(cmd.ExecuteReader());
source.DataSource = dt;
dataGrid1.ItemsSource = source;

在我的数据网格中,我想将DateTime 的列类型更改为String,我该怎么做?


来源是

System.Windows.Forms.BindingSource source = new System.Windows.Forms.BindingSource();

【问题讨论】:

  • 为什么要将 DateTime 转换为字符串?是因为格式吗?您可以通过将格式化选项应用于 XAML 级别的数据网格来实现此目的。

标签: c# .net wpf wpfdatagrid


【解决方案1】:

您可以像这样为DateTime 值定义DataTemplate -

<DataGrid> 
    <DataGrid.Resources> 
        <DataTemplate DataType="{x:Type DateTime}"> 
            <TextBlock Text="{Binding StringFormat={0:d}}"  /> 
        </DataTemplate> 
    </DataGrid.Resources> 
    ... 
</DataGrid> 

参考 - Need to format dates in dynamically built WPF DataGrid

【讨论】:

    猜你喜欢
    • 2018-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 2021-07-26
    相关资源
    最近更新 更多