localized short date for datagridview column headertext c#

Darryl Hoar 181 Reputation points
2024-08-26T20:40:50.9533333+00:00

I need to assign a date (short format) as a datagridview column header text. I have tried many things, including:

DateTime headerdt;

headerdt = myDateTimePicker.Value.AddDays(7);

dataGridView1.Columns[3].HeaderText = headerdt.ToString("d");

Run app on US english system. its ok.

Run on norwegian system and it still displays us shortdate format for column header text.

What am I missing ?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,919 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,010 questions
{count} votes

Accepted answer
  1. Michael Taylor 54,811 Reputation points
    2024-08-26T20:51:12.9+00:00

    Run on norwegian system and it still displays us shortdate format for column header text.

    If you want to show the short format then the format string is correct. You said it works with English. Your wording seems to indicate that it is working for Norwegian as well, but I suspect you are trying to say it isn't.

    Look at the short format for the Norwegian locale on your machine. That is the format it should be using since it gets its formatting rules from the OS. If it isn't then please share with us the short format that is configured for Windows.

    One thing that stands out is that you are doing this programmatically so it should be using the current culture formatting. However with UX stuff it generally uses the UI culture settings instead. These can differ so either ensure you are passing the UI culture to the call to ToString or ensure that both cultures are set to Norwegian AND the short format is set correctly for them. You can see the properties here.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.