【问题标题】:How do i convert date to string variable in WPF using C#?如何使用 C# 在 WPF 中将日期转换为字符串变量?
【发布时间】:2014-04-29 04:56:32
【问题描述】:

我想将日期显示到网格(devexpress)中,其中列 ColumnB 是固定的,它是字符串类型,这里我想将所有类型的数据显示到列中,例如如下所示:

例子:

if(columnname == 'empname')
{
   columnB =  reader["empname"].ToString() != null ? reader["empname"].ToString() : ""; 
}

else if ( columnname == 'empdate')
{
   //my try
 Datetime temp = Convert.ToDateTime(reader["empdate"])!= null ? Convert.ToDateTime(read["empdate"]): null; 
 columnB = temp.ToString();    //columnB is of string type
}

在“else if”部分,我不知道如何将其转换为字符串以显示在网格的同一列中。

【问题讨论】:

  • 你有什么错误吗?
  • 是的!错误是:无法从“System.DateTime?”隐式转换?到“System.DateTime”

标签: c# wpf grid devexpress


【解决方案1】:

你可以试试这个:

<DataGrid AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding TheDate, StringFormat={}{0:MM/dd/yyyy}}" />
    </DataGrid.Columns>
</DataGrid>

如果您不想使用上述方法,那么您可以尝试使用string.Format()

string.Format("{0:dd MM yyyy}", date);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 2016-10-11
    • 2012-10-03
    • 2010-09-23
    • 2022-01-14
    • 2012-03-21
    • 2011-04-29
    相关资源
    最近更新 更多