Connecting Tech Pros Worldwide Forums | Help | Site Map

stupid american system

Sput
Guest
 
Posts: n/a
#1: Nov 15 '05
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?



Morten Wennevik
Guest
 
Posts: n/a
#2: Nov 15 '05

re: stupid american system


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.
--
The hotmail account will most likely not be read, so please respond only
to the news group.
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#3: Nov 15 '05

re: stupid american system


Sput <sputspamnomore@post.htnet.hr> wrote:[color=blue]
> 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?[/color]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Ralph
Guest
 
Posts: n/a
#4: Nov 15 '05

re: stupid american system



"Sput" <sputspamnomore@post.htnet.hr> wrote in message
news:c0qkro$rmk$2@ls219.htnet.hr...[color=blue]
> I am thoroughly frustrated with these seemingly unchangable settings in[/color]
C#.[color=blue]
> For example, fetching data from database sorted by date returns wrong[/color]
order[color=blue]
> (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[/color]
second[color=blue]
> 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?
>[/color]

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


james
Guest
 
Posts: n/a
#5: Nov 15 '05

re: stupid american system


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


"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1a9af2c55ebdedc198a153@msnews.microsoft.c om...[color=blue]
> Sput <sputspamnomore@post.htnet.hr> wrote:[color=green]
> > I am thoroughly frustrated with these seemingly unchangable settings in[/color][/color]
C#.[color=blue][color=green]
> > For example, fetching data from database sorted by date returns wrong[/color][/color]
order[color=blue][color=green]
> > (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[/color][/color]
second[color=blue][color=green]
> > 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?[/color]
>
> 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.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too[/color]


Sput
Guest
 
Posts: n/a
#6: Nov 15 '05

re: stupid american system


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


Michael A. Covington
Guest
 
Posts: n/a
#7: Nov 15 '05

re: stupid american system



"Morten Wennevik" <MortenWennevik@hotmail.com> wrote in message
news:opr3gx0lc7hntkfz@msnews.microsoft.com...[color=blue]
> 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.[/color]

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...


Chuck Conlow
Guest
 
Posts: n/a
#8: Nov 15 '05

re: stupid american system


The first day of *MY* week is now, and always has been MONDAY...
[color=blue]
> My pay period starts on MONDAY
> Everyone in my organization has their calendar start as MONDAY = Work Week[/color]
Starts...[color=blue]
> My pay period ends on SUNDAY
> ad infinitum....[/color]

Chuck

"Michael A. Covington" <look@www.covingtoninnovations.com.for.address> wrote
in message news:eap5asO9DHA.2460@TK2MSFTNGP09.phx.gbl...[color=blue]
>
> "Morten Wennevik" <MortenWennevik@hotmail.com> wrote in message
> news:opr3gx0lc7hntkfz@msnews.microsoft.com...[color=green]
> > 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[/color][/color]
culture.[color=blue]
>
> 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...
>
>[/color]


Richard A. Lowe
Guest
 
Posts: n/a
#9: Nov 15 '05

re: stupid american system


--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
"Sput" <sputspamnomore@post.htnet.hr> wrote in message
news:c0r8a8$mej$1@ls219.htnet.hr...[color=blue]
> as for skeet, programs written in visual basic using same database respond
> correctly[/color]

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.[color=blue]
>
>[/color]


Michael A. Covington
Guest
 
Posts: n/a
#10: Nov 15 '05

re: stupid american system


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).

"Chuck Conlow" <lvnetguy@earthlink.net> wrote in message
news:14gYb.6860$WW3.4339@newsread2.news.pas.earthl ink.net...[color=blue]
> The first day of *MY* week is now, and always has been MONDAY...
>[color=green]
> > My pay period starts on MONDAY
> > Everyone in my organization has their calendar start as MONDAY = Work[/color][/color]
Week[color=blue]
> Starts...[color=green]
> > My pay period ends on SUNDAY
> > ad infinitum....[/color]
>
> Chuck
>
> "Michael A. Covington" <look@www.covingtoninnovations.com.for.address>[/color]
wrote[color=blue]
> in message news:eap5asO9DHA.2460@TK2MSFTNGP09.phx.gbl...[color=green]
> >
> > "Morten Wennevik" <MortenWennevik@hotmail.com> wrote in message
> > news:opr3gx0lc7hntkfz@msnews.microsoft.com...[color=darkred]
> > > 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[/color][/color]
> culture.[color=green]
> >
> > 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...
> >
> >[/color]
>
>[/color]


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#11: Nov 15 '05

re: stupid american system


Sput <sputspamnomore@post.htnet.hr> wrote:[color=blue]
> 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[/color]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Dmitriy Lapshin [C# / .NET MVP]
Guest
 
Posts: n/a
#12: Nov 15 '05

re: stupid american system


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 [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Michael A. Covington" <look@www.covingtoninnovations.com.for.address> wrote
in message news:eap5asO9DHA.2460@TK2MSFTNGP09.phx.gbl...[color=blue]
>
> "Morten Wennevik" <MortenWennevik@hotmail.com> wrote in message
> news:opr3gx0lc7hntkfz@msnews.microsoft.com...[color=green]
> > 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[/color][/color]
culture.[color=blue]
>
> 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...
>
>[/color]

Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#13: Nov 15 '05

re: stupid american system


Dmitriy Lapshin [C# / .NET MVP] <x-code@no-spam-please.hotpop.com>
wrote:[color=blue]
> 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.[/color]

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 Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Dmitriy Lapshin [C# / .NET MVP]
Guest
 
Posts: n/a
#14: Nov 15 '05

re: stupid american system


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

"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1a9c052ceb005f098a162@msnews.microsoft.co m...[color=blue]
> Dmitriy Lapshin [C# / .NET MVP] <x-code@no-spam-please.hotpop.com>
> wrote:[color=green]
> > 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,[/color][/color]
enumerations[color=blue][color=green]
> > cannot change the order of their items in accordance with current[/color][/color]
culture[color=blue][color=green]
> > 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.[/color]
>
> 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 Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too[/color]

Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#15: Nov 15 '05

re: stupid american system


Dmitriy Lapshin [C# / .NET MVP] <x-code@no-spam-please.hotpop.com>
wrote:[color=blue]
> 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.[/color]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Sput
Guest
 
Posts: n/a
#16: Nov 15 '05

re: stupid american system


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


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#17: Nov 15 '05

re: stupid american system


Sput <sputspamnomore@post.htnet.hr> wrote:[color=blue]
> 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[/color]

If you're doing that, it should correctly be sorted to start with,
before you tell the grid to sort the results at all.
[color=blue]
> 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"[/color]

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 - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#18: Nov 15 '05

re: stupid american system


Jon Skeet [C# MVP] <skeet@pobox.com> wrote:[color=blue][color=green]
> > 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"[/color]
>
> 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.[/color]

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);
}
}


--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Sput
Guest
 
Posts: n/a
#19: Nov 15 '05

re: stupid american system


Thank you, your example sorts properly.


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#20: Nov 15 '05

re: stupid american system


Sput <sputspamnomore@post.htnet.hr> wrote:[color=blue]
> Thank you, your example sorts properly.[/color]

So, can you work out what the difference between my example and your
application is?

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Closed Thread