stupid american system

  • Thread starter Thread starter Sput
  • Start date Start date
S

Sput

I am thoroughly frustrated with these seemingly unchangable settings in C#.
For example, fetching data from database sorted by date returns wrong order
(would be correct if I was using american date system). Same goes for
sorting datagrid (sorts dates and
decimal numbers - ie 31.12.2004 is sorted by first number instead of second
and 100.000,00 is treated as text ).
Is there a simple way of changing this or am I to write my own sorting
functions?
 
I don't know if it can be changed, but the same applies to weekday
enumeration which puts sunday as the first day independent of the culture.
 
Sput said:
I am thoroughly frustrated with these seemingly unchangable settings in C#.
For example, fetching data from database sorted by date returns wrong order
(would be correct if I was using american date system). Same goes for
sorting datagrid (sorts dates and
decimal numbers - ie 31.12.2004 is sorted by first number instead of second
and 100.000,00 is treated as text ).
Is there a simple way of changing this or am I to write my own sorting
functions?

Before you start blaming C#:

1) C# is only the language, not the library

2) When you talk about "fetching data from database sorted by date" -
are you sorting by date on the database side, or on the client side? If
it's on the database side, how can it be C#'s fault?

3) Are you actually storing the dates in date columns in the database?

If it's on the client side, it should be possible to see all this in a
simple test program which just populates a data table without actually
going to a database. If you could produce such a program, I'll see what
I can do.
 
Sput said:
I am thoroughly frustrated with these seemingly unchangable settings in C#.
For example, fetching data from database sorted by date returns wrong order
(would be correct if I was using american date system). Same goes for
sorting datagrid (sorts dates and
decimal numbers - ie 31.12.2004 is sorted by first number instead of second
and 100.000,00 is treated as text ).
Is there a simple way of changing this or am I to write my own sorting
functions?

Many routines 'read' the locale settings of the computer and the database.
How they will handle situations as you mentioned is often an interesting
joint-psychosis between the Framework types (and their methods) and Database
storeage types and DBA conventions and settings.

You can start-out by making sure your Windows version is the specific
language/locate version and local settings are setup correctly - as well as
the database. This works if you are only working in one locale all the time.

However, if, as I expect, you are working in a "dual" language/locale
environment, then the most proficient solution IS to create your own sorting
routines and conversion classes (types), as you will likely be continuely
fighting locale differences for some time to come.

-ralph
 
Actually, just add a calculated field like a string made from {fn
YEAR(...) } {fn MONTH(...)}
and then just sort on that column. Have the database do the work for you

JIM
 
if anyone of repliers read this...
i have decide to use clarion's solution for dates
my local settings are set correctly
as for skeet, programs written in visual basic using same database respond
correctly
 
Morten Wennevik said:
I don't know if it can be changed, but the same applies to weekday
enumeration which puts sunday as the first day independent of the culture.

Is there any culture in which Sunday is not the first day of the week?

There may well be cultures that don't use the week, but...
 
The first day of *MY* week is now, and always has been MONDAY...
My pay period starts on MONDAY
Everyone in my organization has their calendar start as MONDAY = Work Week Starts...
My pay period ends on SUNDAY
ad infinitum....

Chuck
 
--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
Sput said:
as for skeet, programs written in visual basic using same database respond
correctly

But that doesn't matter if the query pulling the dates casts them to string
or if the VB application is compensating for the unwanted sorting behaviour
and the dates really are stored as strings in the database / dataset.
Please give some details (i.e. a small sample app) that show the problem and
its context.

R.
 
But that is not a difference between nations. It seems to be a conflicting
usage within the United States. I have seen planning calendars that put
Monday first (so that Saturday and Sunday, which aren't supposed to have any
events, can share a box).
 
Sput said:
if anyone of repliers read this...
i have decide to use clarion's solution for dates
my local settings are set correctly
as for skeet, programs written in visual basic using same database respond
correctly

But you still haven't really told us much about what the situation is.
If you could give us more details, we could see whether clarion's
solution is *actually* the most appropriate (which it may be) or
whether there's a simpler, more elegant solution. It would also help
other people in the same situation in the future.
 
Michael,

I think Monday is the first day of the week in many Eastern European
cultures (at least this is true for Russia and Ukraine). Still, enumerations
cannot change the order of their items in accordance with current culture
just because they are sets of constants by nature. What *should* change
though if the CurrentCulture is set, say, to "ru-RU" is the behavior of
methods such as DayOfWeek which should now return 1 for Monday and 7 for
Sunday.
 
Dmitriy Lapshin said:
I think Monday is the first day of the week in many Eastern European
cultures (at least this is true for Russia and Ukraine). Still, enumerations
cannot change the order of their items in accordance with current culture
just because they are sets of constants by nature. What *should* change
though if the CurrentCulture is set, say, to "ru-RU" is the behavior of
methods such as DayOfWeek which should now return 1 for Monday and 7 for
Sunday.

No, because DateTime.DayOfWeek (which I assume is what you were talking
about) returns a DayOfWeek. If it started returning values which
weren't in the DayOfWeek enumeration, things would go wrong pretty
quickly.

What I believe is *really* missing is a FirstDayOfWeek property in the
System.Globalization.Calendar class. However, it's not really that
simple - the first day of the week doesn't just depend on culture, it
depends on context. A salesman looking at his calendar is likely to
want Monday as the first day of the week. Others (myself included) are
likely to want Sunday as the first day of the week.
 
Jon,

The DateTimeFormatInfo class has the FirstDayOfWeek property which can be
set as well as read. I haven't used it though so I am not sure it does the
job. The MSDN help is very brief on this topic.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE
 
Dmitriy Lapshin said:
The DateTimeFormatInfo class has the FirstDayOfWeek property which can be
set as well as read. I haven't used it though so I am not sure it does the
job. The MSDN help is very brief on this topic.

Goodo - I somehow missed that on previous searches for it (having found
the one on the Calendar control).

Of course, people then need to take note of that property, but at least
it's there.
 
Application is rather large and I noticed problem when I started writting
reports for it.

Data field in database is datetime. When opened in access it can be sorted
properly in it.

When I get it into C#, I use command as
sql="SELECT code,name,item1,item2,date WHERE blah blah ORDER by date"

same happens when using GROUP BY date

In datagrid, as I have asked several times everywhere, which is populated by
data
from same database, when I click on column that contains dates or decimal
numbers, it
is sorted just by "dd" of "dd.MM.yyyy", while decimal is sorted by "#" of
"#,##0.00"

I have tried formating date columns to "dd.MM.yyyy", set my culture info to
current thread etc etc.


Current solution:
grab data from datatable to array, sort it there, then populate
datagrid.Easy, but possibly slow
 
Sput said:
Application is rather large and I noticed problem when I started writting
reports for it.

Data field in database is datetime. When opened in access it can be sorted
properly in it.

When I get it into C#, I use command as
sql="SELECT code,name,item1,item2,date WHERE blah blah ORDER by date"

same happens when using GROUP BY date

If you're doing that, it should correctly be sorted to start with,
before you tell the grid to sort the results at all.
In datagrid, as I have asked several times everywhere, which is populated by
data from same database, when I click on column that contains dates or decimal
numbers, it is sorted just by "dd" of "dd.MM.yyyy", while decimal is sorted by "#" of
"#,##0.00"

It sounds like it's DataGrid's (or possibly even DataView's) sorting
that's at fault then, sorting it by formatted string rather than by
actual value. I'll look into it a bit... it sounds like there's no need
for an actual database to be involved at all.
 
Jon Skeet said:
It sounds like it's DataGrid's (or possibly even DataView's) sorting
that's at fault then, sorting it by formatted string rather than by
actual value. I'll look into it a bit... it sounds like there's no need
for an actual database to be involved at all.

Hmm... I can't reproduce the problem. Here's an example which shows a
DataGrid which allows sorting of DateTimes and decimals with no
problems:

using System;
using System.Data;
using System.Windows.Forms;
using System.Drawing;

public class Test
{
[STAThread]
static void Main(string[] args)
{
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("DateTime", typeof(DateTime));
table.Columns.Add("Decimal", typeof(decimal));

for (int i=0; i < 20; i++)
{
table.Rows.Add(new object[]{i,
MakeRandomDateTime(),
MakeRandomDecimal()});
}

Form form = new Form();
form.Text = "DataGrid test";
form.Size = new Size (300, 600);
DataGrid grid = new DataGrid();
grid.DataSource = table;
grid.Dock = DockStyle.Fill;
form.Controls.Add(grid);
Application.Run(form);
}

static Random rng = new Random();
static DateTime MakeRandomDateTime()
{
return new DateTime (rng.Next(5)+1998,
rng.Next(12)+1,
rng.Next(25)+1,
rng.Next(24),
rng.Next(60),
rng.Next(60));
}

static decimal MakeRandomDecimal()
{
// Horrible, I know...
int whole = rng.Next (10000);
int fractional = rng.Next(10000);
string combined = String.Format("{0}.{1:d4}",
whole,
fractional);
return decimal.Parse(combined);
}
}
 
Back
Top