Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / DataGrid / October 2007

Tip: Looking for answers? Try searching our database.

Datagrid default date format

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Devron Blatchford - 08 Oct 2007 03:22 GMT
Hi there,

I have a small date formatting issue with the datagrid control. I have a
page that dynamically binds results from a datatable. The number/names of
columns and datatypes can vary. I use the datagrid to return the results to
the client so they can be exported to excel. I have recently moved this aspx
application to another server and the date columns are now returning to the
client in mm/dd/yy format where as they were returning in dd/mm/yy which is
what I want. I unserstand that I can set the format on columns but in this
example the columns are dynamic and the datagrid creates them on the fly. I
have set all international setting for all users including the default user
to the format I want but it still returns to the client incorrectly. I have
also restarted IIS and the server after these changes. My question is
how/where do I set the date format so when the datagrid dynamically binds a
date field it will globally set the format of that column to the format I
specify as it did on the previous server that this app resided on? Any help
would be appreciated.

Binding code example below. very simple.

Thanks

Devron

dt = GetExtractDataTable(mintExtractID)
dgExtract.DataSource = dt
dgExtract.DataBind()

Signature

Devron Blatchford

Walter Wang [MSFT] - 08 Oct 2007 10:47 GMT
Hi Devron,

Based on my understanding, your objective here is to set DataFormatString
of auto-generated columns (DataGrid.AutoGenerateColumns = true) of
DataGrid. Please feel free to let me know if I've misunderstood anything.

Currently it's not easy to achieve this objective using simple property
setting, since the auto-generated columns are not added to DataGrid.Columns
collection and they're not accessible from outside. We have to inherit from
DataGrid and override CreateColumnSet to get references to the
auto-generated columns:

namespace myns
{
   public class MyDataGrid : DataGrid
   {
       protected override System.Collections.ArrayList
CreateColumnSet(PagedDataSource dataSource, bool useDataSource)
       {
           ArrayList al = base.CreateColumnSet(dataSource, useDataSource);
           foreach (DataGridColumn col in al)
           {
               BoundColumn bc = col as BoundColumn;
               if (bc != null)
               {
                   bc.DataFormatString = "{0:dd/MM/yyyy}";
               }
           }
           return al;
       }
   }

In your ASPX:

<%@ Register TagPrefix="c" Namespace="myns" Assembly="__code"  %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
   <title>Untitled Page</title>
</head>
<body>
   <form id="form1" runat="server">
   <div>
       <c:MyDataGrid ID="grid1" runat="server"></c:MyDataGrid>

(Assembly="__code" assumes you're using MyDataGrid from a class file in
App_Code)

Also note this is a quick and dirty hack without checking for the column's
actual data type, you may want to use Reflector
(http://www.aisto.com/roeder/dotnet/) to insepect implementation of
DataGrid.CreateAutoGeneratedColumns to see how to get each column's binding
data's type.

Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Devron Blatchford - 09 Oct 2007 00:06 GMT
Thanks Walter But.....

This seems overly complicated. It was working on the server that I moved it
from which leads me to beleive there must be a setting that the date format
defaults from somewhere?

I failed to mention previously that both the servers I moved from/to are
Server2003 and running .NET 1.1 app.

All settings on the servers I can see have the date format in d/mm/yy set.
I am not keen to change the source code if it is not required so I was
hoping to find the formatting setting somewhere or somehow force ASP.NET to
use what I specify by default.

Thanks Again.

Devron Blatchford
Devron Blatchford - 09 Oct 2007 04:21 GMT
Ok, I fixed this by setting the culture string in the header of the aspx file:

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="RunExtract.aspx.vb" Inherits="DEX.RunExtract" Culture="en-AU"%>

Thanks

Signature

Devron Blatchford

> Hi Devron,
>
[quoted text clipped - 63 lines]
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
Walter Wang [MSFT] - 09 Oct 2007 07:19 GMT
Hi Devron,

Thanks for your update.

I was just about to post the information about setting Culture specifically
in your page or web.config. Though please note that setting Culture will
determine the results of culture-dependent functions (such as date, number,
and currency formatting, etc.). Depending on your actual requirement, this
may or may not have impact on your other functionality of your web
application.

#How to: Set the Culture and UI Culture for ASP.NET Web Page Globalization
http://msdn2.microsoft.com/en-us/library/bz9tc508.aspx

Please feel free to let me know if you have anything unclear.

Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.