Formatting a date in a gridview with auto-generated fields

Discussion in 'ASP .Net' started by BillE, Jun 15, 2007.

  1. BillE

    BillE Guest

    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
    BillE, Jun 15, 2007
    #1
    1. Advertising

  2. 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
    >
    >
    >
    >
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Jun 16, 2007
    #2
    1. Advertising

  3. BillE

    BillE Guest

    Thank you kindly.

    "Milosz Skalecki [MCAD]" <> wrote in message
    news:...
    > 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
    >>
    >>
    >>
    >>
    BillE, Jun 18, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,226
    Michael Borgwardt
    May 30, 2004
  2. Jason
    Replies:
    3
    Views:
    17,023
    Jan Hyde
    May 8, 2006
  3. linkswanted
    Replies:
    1
    Views:
    879
  4. Iluvatar
    Replies:
    3
    Views:
    3,247
    Iluvatar
    Jun 4, 2008
  5. JohnE
    Replies:
    2
    Views:
    2,054
    JohnE
    Sep 22, 2009
Loading...

Share This Page