C# 将字符串转换为日期类型

C# 将字符串转换为日期类型,c#,wpf,C#,Wpf,我想在wpf应用程序中将字符串转换为日期类型。下面是代码 Connection con1 = new Connection(); cmbStatementDate.Items.Clear(); con1.dataGet("SELECT distinct statement_date from stockstatement ORDER BY statement_date asc"); DataTable dt1 = new DataTable(); con1.sda.Fill(dt1); for

我想在wpf应用程序中将字符串转换为日期类型。下面是代码

Connection con1 = new Connection();
cmbStatementDate.Items.Clear();
con1.dataGet("SELECT distinct  statement_date from stockstatement ORDER BY statement_date asc");
DataTable dt1 = new DataTable();
con1.sda.Fill(dt1);
foreach (DataRow dr1 in dt1.Rows)
{
    cmbStatementDate.Items.Add(dr1["statement_date"].ToString());
}
如何将其结果转换为日期格式DD MM YYYY并保存到cmbStatementDatecombobox


提前感谢。

您可以像这样编辑代码

Connection con1 = new Connection();
cmbStatementDate.Items.Clear();
con1.dataGet("SELECT distinct  statement_date from stockstatement ORDER BY statement_date asc");
DataTable dt1 = new DataTable();
con1.sda.Fill(dt1);
foreach (DataRow dr1 in dt1.Rows)
{
     cmbStatementDate.Items.Add(Convert.ToDateTime(dr1["statement_date"]).ToString("DD-MM-YYYY"));
}

您的意思是它已经是表上的日期\日期时间类型,并且您希望将其视为dd-MM-yyyy字符串吗?如果是:

((DateTime)dr1["statement_date"]).ToString("dd-MM-yyyy");

可以。我假设该字段是datetime或date字段。如果字段为varchar,则应在SQL查询中将其解析为datetime或date

不需要将日期转换为字符串,甚至不需要逐个添加项目。可以使用数据绑定直接绑定到DataTable

Windows窗体

数据绑定组合框如中所述

还可以使用FormatString属性指定控件应如何显示日期,例如:

//Use local short date format
cmbStatementDate.FormatString = "d";
//or hard-coded short format
//  cmbStatementDate.FormatString = "MM-dd-YYYY";
cmbStatementDate.DisplayMember = "statement_date";
cmbStatementDate.DataSource=dt;
不过,指定FormatString和DisplayMember的最佳位置是表单设计器本身,而不是代码隐藏文件。这使得本地化和显示格式更加容易

数据绑定意味着每次添加新项时也不需要重新绘制组合框。只有在加载所有数据后,才会重新绘制整个组合

还可以从设置或资源文件中提取控件属性。这将允许用户通过创建新的设置或资源文件来本地化应用程序

一般来说,本地化、全球化和数据绑定自2002年以来就进入了.NET。只需创建一次应用程序,就可以轻松地将其本地化为不同的标记,而无需手动格式化字符串

有关数据绑定的一般信息,请参见:

一点警告

不要使用文档中显示的数据绑定顺序:

cmbStatementDate.DataSource=dt;
cmbStatementDate.DisplayMember = "statement_date";
如果可能,这将使用原始的DisplayMember值重新绘制控件两次

WPF

数据绑定和格式字符串也可以在WPF中工作,并且可能更易于使用。在不使用数据绑定的情况下,可以使用以下内容创建格式化的组合框:

<ComboBox x:Name="cmbStatementDate" 
          DisplayMemberPath="statement_date" ItemStringFormat="d" />
这段代码看起来像数据绑定,但事实并非如此

通过数据绑定,组合可以绑定到codebehind或ViewModel类中的属性,例如:

<ComboBox x:Name="cmbStatementDate" 
          DisplayMemberPath="statement_date" 
          ItemStringFormat="dd-MM-yyyy" 
          ItemsSource="{Binding Path=MyDateData}"
          SelectedValue="{Binding Path=SelectedDate}" />
在本例中,MyDateData和SelectedDate引用codebehind或ViewModel中的属性


WPF中的数据绑定如所述

数据库中的statement\u date列真的是字符串类型而不是真正的日期吗?日期没有格式。它们是二进制值。完全移除。ToString。最好的情况是,statement_date是一个文本字段,而ToString什么也不做。最糟糕的情况是,它将有效的DateTime对象转换为字符串,需要将其解析回DateTime。请向我们展示stockstatement的创建表脚本。对您有用吗?表的架构是什么?您使用的是什么桌面堆栈?正如mjwills指出的那样,WPF组合具有StringFormat属性
cmbStatementDate.ItemsSource=dt1;
<ComboBox x:Name="cmbStatementDate" 
          DisplayMemberPath="statement_date" 
          ItemStringFormat="dd-MM-yyyy" 
          ItemsSource="{Binding Path=MyDateData}"
          SelectedValue="{Binding Path=SelectedDate}" />