仅显示自动生成的网格视图中的日期WPF C#

时间:2022-01-31 14:10:20

I am working on WPF where I use auto generated grid view... date time that stored in a SQL Server 2014 database is in format 2015-08-30 but when it shows on auto generated it shows up as 2015-08-30 12:00:00 AM

我正在使用自动生成的网格视图的WPF ...存储在SQL Server 2014数据库中的日期时间格式为2015-08-30,但是当它显示在自动生成时它显示为2015-08-30 12 :00:00 AM

I want to display it as 2015-08-30 only

我想将它显示为2015-08-30

string ConString = // my Connection string
string CmdString = string.Empty;

using (SqlConnection con = new SqlConnection(ConString))
{
    CmdString = "SELECT * FROM [dbo].[Client_Info_Detail]";

    SqlCommand cmd = new SqlCommand(CmdString, con);
    SqlDataAdapter sda = new SqlDataAdapter(cmd);

    DataTable dt = new DataTable("Client");
    sda.Fill(dt);

    grdClient.ItemsSource = dt.DefaultView;
}

3 个解决方案

#1


2  

I had the same problem and find this code-behind working: You have to set this event for your datagrid:

我有同样的问题,发现这个代码隐藏工作:你必须为您的数据网格设置此事件:

private void yourDatagrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.PropertyType == typeof(DateTime))//if a column is typeof DateTime
    {
        (e.Column as DataGridTextColumn).Binding.StringFormat = "yyyy-MM-dd";//set your date format 
    }
}

This will format any Date column in your datagrid in the format.

这将格式化数据网格中的任何Date列。

#2


2  

You should be able to do something like this:

你应该可以做这样的事情:

<dgv:DataGridTextColumn Binding="{Binding YourDate, StringFormat=\{0:yyyy-mm-dd\}}" />

#3


2  

You can set the date formatting directly in your XAML with help of the StringFormat property of Binding (System.Windows.Data.Binding):

您可以借助Binding的StringFormat属性(System.Windows.Data.Binding)直接在XAML中设置日期格式:

<DataGridTextColumn Header="Date" Binding="{Binding Path=Date, StringFormat='yyyy-MM-dd'}" />

#1


2  

I had the same problem and find this code-behind working: You have to set this event for your datagrid:

我有同样的问题,发现这个代码隐藏工作:你必须为您的数据网格设置此事件:

private void yourDatagrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.PropertyType == typeof(DateTime))//if a column is typeof DateTime
    {
        (e.Column as DataGridTextColumn).Binding.StringFormat = "yyyy-MM-dd";//set your date format 
    }
}

This will format any Date column in your datagrid in the format.

这将格式化数据网格中的任何Date列。

#2


2  

You should be able to do something like this:

你应该可以做这样的事情:

<dgv:DataGridTextColumn Binding="{Binding YourDate, StringFormat=\{0:yyyy-mm-dd\}}" />

#3


2  

You can set the date formatting directly in your XAML with help of the StringFormat property of Binding (System.Windows.Data.Binding):

您可以借助Binding的StringFormat属性(System.Windows.Data.Binding)直接在XAML中设置日期格式:

<DataGridTextColumn Header="Date" Binding="{Binding Path=Date, StringFormat='yyyy-MM-dd'}" />