我正在显示有关案例的一些信息,这些案例已经存在于 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/