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 / General / June 2007

Tip: Looking for answers? Try searching our database.

Formatting a date in a gridview with auto-generated fields

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
BillE - 15 Jun 2007 19:15 GMT
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
Milosz Skalecki [MCAD] - 16 Jun 2007 00:53 GMT
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

Signature

Milosz

> I am populating a gridview using auto-generated fields, because I need to
> enable sorting.
[quoted text clipped - 5 lines]
> Thanks
> Bill
BillE - 18 Jun 2007 15:09 GMT
Thank you kindly.

> Hi there Billie,
>
[quoted text clipped - 54 lines]
>> Thanks
>> Bill

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.