2

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

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

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

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

我尝试更改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();

提前,谢谢!

4

2 回答 2

1

尝试这个:

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

<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

于 2013-08-13T10:46:03.397 回答
0

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

<DataGrid ItemsSource="{Binding Items}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Value" Binding="{Binding Value, 
            StringFormat={}{0:dd/MM/yyyy}}" />
    </DataGrid.Columns>
</DataGrid>
于 2013-08-13T10:37:11.470 回答