Reputation: 500
I'm displaying some information about case which already exist in a DataGrid
.
In my MS Access
database all the date are stored in a DateTime
format and looks like this : dd/MM/yyyy.
The problem is that when displayed on the DataGrid
when the program runs, the format change to MM/dd/yyyy.
The thing that makes me lost is that when I display in a MessageBox
the content of the concerned cells just before assign the DataTable
to the DataGrid
I've got the right format (dd/MM/yyyy).
I've tried to change the DateTimeMode
but the same format is displayed.
Someone got any tips who to fix this problem ? I give you the code below :
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
Actually the DataGrid is declared like this in the xaml file :
<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>
I'm binding the DataTable with: existingCase.ItemsSource = temp.AsDataView();
In advance, thanks !
Upvotes: 2
Views: 2020
Reputation: 22702
Try this:
Add Startup
event handle in App.xaml
file:
<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" ... />
In App.xaml.cs
add this:
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)));
}
}
Now, the date should be displayed depending on the current culture.
Note:
StringFormat
suitable only for display the date. If you want to edit a cell with a date, such as the format dd/MM/yyyy
, entering data in a way dd/MM/yyyy
, the system will be expected format MM/dd/yyyy
, so the error will appear as a red Border
.
Upvotes: 1
Reputation: 69959
Without displaying your code for the column in the DataGrid
that displays the DateTime
object, it is hard to answer, but I will assume that you are binding the value. To set a string format on the binding, you would do something like this:
<DataGrid ItemsSource="{Binding Items}">
<DataGrid.Columns>
<DataGridTextColumn Header="Value" Binding="{Binding Value,
StringFormat={}{0:dd/MM/yyyy}}" />
</DataGrid.Columns>
</DataGrid>
Upvotes: 0