在datagrid中更改类型列

时间:2012-07-13 07:56:36

标签: c# .net wpf wpfdatagrid

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();

1 个答案:

答案 0 :(得分:0)

您可以为DataTemplate值定义DateTime -

<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