all groups > asp.net datagrid control > october 2007 >
You're in the

asp.net datagrid control

group:

Datagrid default date format


Datagrid default date format Devron Blatchford
10/7/2007 7:22:03 PM
asp.net datagrid control: 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()

--
RE: Datagrid default date format wawang NO[at]SPAM online.microsoft.com (
10/8/2007 9:47:52 AM
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.
RE: Datagrid default date format Devron Blatchford
10/8/2007 4:06:01 PM
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.

RE: Datagrid default date format Devron Blatchford
10/8/2007 8:21:00 PM
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

--
Devron Blatchford


[quoted text, click to view]
RE: Datagrid default date format wawang NO[at]SPAM online.microsoft.com (
10/9/2007 12:00:00 AM
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.
AddThis Social Bookmark Button