Formatting GridView

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nice.com@gmail.com

    #1

    Formatting GridView

    Hi,

    It it possible to alter properties of a gridview control based on the
    data it holds?

    Specifically, I would like to alter colours held in the data. For
    example....here 's the data

    Surname Forename Registered RecentContact
    Smith Henry no no
    Jones Wesley yes no
    Adams Sarah yes yes

    I would like to display records in red for those unregistered.. yellow
    for those registered without recent contact.. and green for those
    registered with recent contact.

    Can this be done simply?

  • Eliyahu Goldin

    #2
    Re: Formatting GridView

    Yes. The most flexible way is to handle the RowDataBound event. In the event
    you can access the values of the columns and set any visual properties as
    you wish.

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]




    <nice.com@gmail .comwrote in message
    news:1176299889 .271536.139960@ w1g2000hsg.goog legroups.com...
    Hi,
    >
    It it possible to alter properties of a gridview control based on the
    data it holds?
    >
    Specifically, I would like to alter colours held in the data. For
    example....here 's the data
    >
    Surname Forename Registered RecentContact
    Smith Henry no no
    Jones Wesley yes no
    Adams Sarah yes yes
    >
    I would like to display records in red for those unregistered.. yellow
    for those registered without recent contact.. and green for those
    registered with recent contact.
    >
    Can this be done simply?
    >

    Comment

    • marss

      #3
      Re: Formatting GridView


      nice.com@gmail. com wrote:
      Hi,
      >
      It it possible to alter properties of a gridview control based on the
      data it holds?
      >
      Specifically, I would like to alter colours held in the data. For
      example....here 's the data
      >
      Surname Forename Registered RecentContact
      Smith Henry no no
      Jones Wesley yes no
      Adams Sarah yes yes
      >
      I would like to display records in red for those unregistered.. yellow
      for those registered without recent contact.. and green for those
      registered with recent contact.
      >
      Can this be done simply?
      In the OnRowDataBound event handler you have the all required data to
      do this.
      protected void GridView1_RowDa taBound(object sender,
      GridViewRowEven tArgs e)
      {.....

      Here e.Row is a row of the GridView and e.Row.DataItem is an object
      you bind to the GridView.
      Suppose, you bind an object Person with the property Registered. To
      paint row in red you need something like this:
      Person p = (Person)e.Row.D ataItem;
      if (!p.Registered)
      e.Row["background-color"] = "red";

      Regards

      Comment

      • bpd

        #4
        Re: Formatting GridView

        On Apr 11, 9:58 am, nice....@gmail. com wrote:
        Hi,
        >
        It it possible to alter properties of a gridview control based on the
        data it holds?
        >
        Specifically, I would like to alter colours held in the data. For
        example....here 's the data
        >
        Surname Forename Registered RecentContact
        Smith Henry no no
        Jones Wesley yes no
        Adams Sarah yes yes
        >
        I would like to display records in red for those unregistered.. yellow
        for those registered without recent contact.. and green for those
        registered with recent contact.
        >
        Can this be done simply?
        Use the gridview RowDataBound event.

        in C#:

        <gridview id>_RowDataBoun d(object sender, GridViewRowEven tArgs e)
        {
        if (e.Row.RowType == DataControlRowT ype.DataRow)
        {
        //non-edit mode
        if ((e.Row.RowStat e == DataControlRowS tate.Normal) ||
        (e.Row.RowState == DataControlRowS tate.Alternate) )
        {
        <your code to alter row color here for example:
        lbl = (Label)e.Row.Ce lls[1].Controls[1];
        (obviously, lbl is defined as a label --- Label lbl;)
        lbl.ForeColor = Color.Red; (or yellow or
        green) to set the text color
        --or--
        e.Row.Cells[1].BackColor = Color.Red; (or
        yellow or green) to set the color of the cell
        }
        }
        }

        Comment

        • marss

          #5
          Re: Formatting GridView


          marss wrote:
          e.Row["background-color"] = "red";
          Excuse for the misprint. Correction:
          e.Row.Style["background-color"] = "red";

          Comment

          Working...