【问题标题】:DataGrid displaying the wrong dateformatDataGrid 显示错误的日期格式
【发布时间】:2013-08-13 18:51:13
【问题描述】:

我正在显示一些关于 DataGrid 中已经存在的案例的信息。

在我的MS Access 数据库中,所有日期都以DateTime 格式存储,如下所示:dd/MM/yyyy。

问题是当程序运行时显示DataGrid时,格式变为MM/dd/yyyy。

让我迷失的是,当我在MessageBox 中显示相关单元格的内容时,就在将DataTable 分配给DataGrid 之前,我得到了正确的格式(dd/MM/yyyy )。

我尝试更改DateTimeMode,但显示的格式相同。

有人有任何提示可以解决这个问题吗?我给你下面的代码:

数据表 temp = dt.Clone();

            temp = (from nardin in dt.AsEnumerable().Distinct()
                    where nardin["NUMERO CLIENT"].ToString() == _centerID.ToString()
                    select nardin).CopyToDataTable();





            RemoveDuplicates(temp, "NUMERO DOSSIER");


            foreach (DataRow row in temp.Rows)
            {
                MessageBox.Show(row["DATE INTERVENTION"].ToString()); //this gives me the DateTime format i'd like to display
            }

            existingCase.ItemsSource = temp.AsDataView(); //once assigned to the DataGrid the DateTime format is not the same as above

实际上 DataGrid 在 xaml 文件中是这样声明的:

   <DataGrid  SelectionUnit="FullRow" SelectedItem="{Binding SelectedBI, Mode=TwoWay}" AutoGenerateColumns="True"   Margin="0,167,12,167" Name="existingBI"  Width="588" HorizontalAlignment="Right">
        <DataGrid.RowStyle>
            <Style TargetType="{x:Type DataGridRow}">
                <EventSetter Event="MouseDoubleClick" Handler="resultDataGridBI_MouseDoubleClick"/>
            </Style>
        </DataGrid.RowStyle>
    </DataGrid>

我将 DataTable 绑定到:existingCase.ItemsSource = temp.AsDataView();

提前,谢谢!

【问题讨论】:

    标签: c# wpf datetime datagrid


    【解决方案1】:

    试试这个:

    App.xaml文件中添加Startup事件句柄:

    <Application x:Class="DataGridAddRows.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="Application_Startup" ... />
    

    App.xaml.cs 中添加:

    public partial class App : Application
    {
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement),
                new FrameworkPropertyMetadata(System.Windows.Markup.XmlLanguage.GetLanguage(System.Globalization.CultureInfo.CurrentCulture.IetfLanguageTag)));
        }
    }            
    

    现在,日期应根据当前文化显示。

    Note: StringFormat适合显示日期。如果要编辑带有日期的单元格,例如dd/MM/yyyy的格式,以dd/MM/yyyy的方式输入数据,系统会预期格式MM/dd/yyyy,所以错误会显示为红色Border

    【讨论】:

    • 好的,它与我设置日期时间文化的线程完美配合!谢谢你!
    【解决方案2】:

    如果不显示 DataGrid 中显示 DateTime 对象的列的代码,很难回答,但我会假设您正在绑定该值。要在绑定上设置字符串格式,您可以执行以下操作:

    <DataGrid ItemsSource="{Binding Items}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Value" Binding="{Binding Value, 
                StringFormat={}{0:dd/MM/yyyy}}" />
        </DataGrid.Columns>
    </DataGrid>
    

    【讨论】:

    • 我引用了 dg 声明。我尝试添加您的解决方案,但结果相同。
    猜你喜欢
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多