Change Row Color of Gridview in ASP.NET 2.0


Method – 1 : DataBound Event

This article is used to Change Color of Gridview datarow when data is binding.

When Date in Gridview datarow’s field is today’s date.

Note : lblPostedOn is label which boundfield which is of date type.

 protected void gvQuery_DataBound(object sender, EventArgs e)
    {
        Label sDate = null;

        foreach (GridViewRow r in gvQuery.Rows)
        {
            sDate = (Label)r.FindControl("lblPostedOn");
            System.DateTime dt = Convert.ToDateTime(sDate.Text);
            if (dt.Date == System.DateTime.Today.Date)
                r.BackColor = System.Drawing.Color.Gray;
        }
    }

Method – 2 : RowCreated Event

Here’s the code for that :

 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        e.Row.Attributes.Add("onMouseOver", "this.style.background='#eeff00'");
        e.Row.Attributes.Add("onMouseOut", "this.style.background='#ffffff'");
    }
Advertisements

2 thoughts on “Change Row Color of Gridview in ASP.NET 2.0

  1. Pingback: Consulting

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s