c# - DataGrid 显示错误的日期格式

标签 c# wpf datetime datagrid

我正在显示有关案例的一些信息,这些案例已经存在于 DataGrid 中.

在我的 MS Access数据库中所有的日期都存储在 DateTime 中格式,看起来像这样:dd/MM/yyyy。

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

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

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

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

DataTable 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 绑定(bind)为:existingCase.ItemsSource = temp.AsDataView();

提前谢谢!

最佳答案

试试这个:

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)));
    }
}            

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

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

关于c# - DataGrid 显示错误的日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18206581/

相关文章:

WPF MVVM 与特定类型绑定(bind)

c# - WPF System.ComponentModel.Win32Exception (0x80004005) : Invalid window handle

C# 是否可以将 DateTime 格式转换为整数或 float ?

sql-server - 由于区域设置,将字符串转换为日期时间时出错

c# - 将 xml 绑定(bind)到模型类

c# - System.Web.HttpRequest.FillInFormCollection() 和 System.Web.HttpRequest.GetEntireRawContent() 非常慢

c# - 以编程方式在加载的松散 xaml 文件中的控件上设置文本

datetime - 使用 DateTimeStyles.AssumeUniversal 时,为什么 JsonConvert 会使用 DateTimeKind.Unspecified 更改 DateTimes 的时间?

c# - 在 Quartz.Net 中调度依赖作业

c# - 允许用户即时向其数据库添加列的最佳方法是什么?