Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Formatting a date in a gridview with auto-generated fields

Reply
Thread Tools

Formatting a date in a gridview with auto-generated fields

 
 
BillE
Guest
Posts: n/a
 
      06-15-2007
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



 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      06-15-2007
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
>
>
>
>

 
Reply With Quote
 
 
 
 
BillE
Guest
Posts: n/a
 
      06-18-2007
Thank you kindly.

"Milosz Skalecki [MCAD]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>>
>>
>>
>>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JasperReports - formatting currency and date fields Dave Java 1 02-19-2007 12:38 PM
gridview, databinding, date and formatting problem tarscher@gmail.com ASP .Net 12 12-27-2006 01:55 PM
Formatting my date in the GridView :-( Simon Harvey ASP .Net 3 02-15-2006 10:34 AM
Formatting a GridView date? David Veeneman ASP .Net 2 01-26-2006 04:04 PM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM



Advertisments