【问题标题】:C# WPF - Formatting a datagrid after importing from csvC# WPF - 从 csv 导入后格式化数据网格
【发布时间】:2019-02-15 16:44:28
【问题描述】:

我已经导入了一个 csv 数据表,并且所有列都被格式化为字符串。有没有办法在导入后将格式列值更改为短日期或货币?任何帮助表示赞赏。

【问题讨论】:

  • 你做了什么,你到底想做什么。包括对所有相关内容的解释,这样人们就不会浪费时间提出建议来找到你,然后没有提及意味着不会工作的事情。 stackoverflow.com/help/mcve
  • 你是说DataTable中的所有列都是字符串类型的吗?您没有任何 DateTimeDecimal 列?

标签: c# wpf datagrid string-formatting import-csv


【解决方案1】:

您可以通过StringFormat 做到这一点

 <DataGrid x:Name="MyData" VerticalAlignment="Top" Margin="2,2,3,0" ItemsSource="{Binding Items}" HeadersVisibility="Column" AutoGenerateColumns="False" CanUserDeleteRows="False">
      <DataGrid.Columns>
            <DataGridTextColumn Header="Date" Width="auto" Binding="{Binding Date, StringFormat=M/d/yyyy}"/>
            <DataGridTextColumn Header="Price" Width="auto" Binding="{Binding Price, StringFormat=C}"/>
      </DataGrid.Columns>
 </DataGrid>

可以查看字符串格式类https://docs.microsoft.com/en-us/dotnet/api/system.string.format?view=netframework-4.7.2

【讨论】:

    猜你喜欢
    • 2011-12-20
    • 2012-09-14
    • 2020-03-03
    • 2020-05-17
    • 2017-06-16
    • 1970-01-01
    • 2014-12-28
    • 2016-11-29
    • 1970-01-01
    相关资源
    最近更新 更多