473,473 Members | 2,303 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Formatting a date in a gridview with auto-generated fields

I am populating a gridview using auto-generated fields, because I need to
enable sorting.

I would like to format a column containing dates in the gridview.

How is this done?

Thanks
Bill

Jun 15 '07 #1
2 4963
Hi there Billie,

you have to handle RowDataBound event
-- aspx page --

<asp:GridView runat="server" ID="gridView"
AutoGenerateColumns="true"
OnRowDataBound="gridView_RowDataBound">

-- end aspx page --
-- code beside --

protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow row = ((DataRowView)e.Row.DataItem).Row;

int index = GetDateColumnIndex(row);

e.Row.Cells[index].Text = ((DateTime)
row[index]).ToString("dd/MM/yyyyyyyy-MM-dd");
}
}

private int dateColumnIndex = -1;
private int GetDateColumnIndex(DataRow row)
{
if (this.dateColumnIndex == -1)
{
this.dateColumnIndex =
row.Table.Columns.IndexOf("ItemRecieved");

if (dateColumnIndex < 0)
{
throw new Exception(
"datasource does not contain the MyDateColumn column");
}
}
return this.dateColumnIndex;
}

-- end code beside --
Hope this helps

--
Milosz
"BillE" wrote:
I am populating a gridview using auto-generated fields, because I need to
enable sorting.

I would like to format a column containing dates in the gridview.

How is this done?

Thanks
Bill

Jun 16 '07 #2
Thank you kindly.

"Milosz Skalecki [MCAD]" <mi*****@DONTLIKESPAMwp.plwrote in message
news:A4**********************************@microsof t.com...
Hi there Billie,

you have to handle RowDataBound event
-- aspx page --

<asp:GridView runat="server" ID="gridView"
AutoGenerateColumns="true"
OnRowDataBound="gridView_RowDataBound">

-- end aspx page --
-- code beside --

protected void gridView_RowDataBound(object sender, GridViewRowEventArgs
e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow row = ((DataRowView)e.Row.DataItem).Row;

int index = GetDateColumnIndex(row);

e.Row.Cells[index].Text = ((DateTime)
row[index]).ToString("dd/MM/yyyyyyyy-MM-dd");
}
}

private int dateColumnIndex = -1;
private int GetDateColumnIndex(DataRow row)
{
if (this.dateColumnIndex == -1)
{
this.dateColumnIndex =
row.Table.Columns.IndexOf("ItemRecieved");

if (dateColumnIndex < 0)
{
throw new Exception(
"datasource does not contain the MyDateColumn column");
}
}
return this.dateColumnIndex;
}

-- end code beside --
Hope this helps

--
Milosz
"BillE" wrote:
>I am populating a gridview using auto-generated fields, because I need to
enable sorting.

I would like to format a column containing dates in the gridview.

How is this done?

Thanks
Bill


Jun 18 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: euan | last post by:
HI Guys, I have bee using conditional formatting in the datagrid recently and I am moving over to framework 2.0 and noticed the datagrid has been replaced by the gridview. So, I would like to do...
2
by: David Veeneman | last post by:
How does one format a date column in a GridView control? I had assumed that the DataFormat string would do it, but MSDN only shows numeric formatting codes. Can dates be formatted using that...
3
by: Simon Harvey | last post by:
Hi all, I'm having problems getting my date to format. Someone told me that with the GridView, you need to use a TemplateColumn and not a BoundColumn when displaying dates. Given that, can...
2
by: J055 | last post by:
Hi I've been looking into different ways of formatting columns/rows in the GridView control. I realize now, I think, that once the datasource is bound to the control the original column type...
2
by: John | last post by:
Hi I have applied auto format to a gridview. Now I would like to change the font size without effecting the rest of the auto formatting. I have tried setting the font size in grid view...
4
by: Ken Wigle | last post by:
All, I would be very grateful for any help on this question. I have an application in asp.net 2.0 where I dynamically create a datatable and then bind that to a gridview. Unfortunately, the...
3
by: David | last post by:
I am using .Net 2.0 and VS2005 with SQL server db. The datetime columns on SQL have the date and time in them. How can I display just the short date format (e.g. a datetime of 10/19/2006 11:23:57...
12
by: tarscher | last post by:
Hi all, A simple problem that seems hard to solve... I have a gridview with a date row. <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("time") %>'></asp:Label>...
0
by: Don Miller | last post by:
I'd like to use a programmatically bound GridView to display a dataset (or work with a datareader) that may include everchanging numbers of database columns, in unpredictable order, with varying...
2
by: tomh2099 | last post by:
Hi, Hi, I have an ASP.NET 2005 application (using VB) with a GridView control that needs to have the last 5 or 6 rows in Bold or maybe some other special formatting. Most of the rows show...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.