Ramani Sandeep's Blog

DotNetting – Fast , Easy Way of Developing Applications

Posts Tagged ‘Change Row Color of Gridview’

Change Row Color of Gridview in ASP.NET 2.0

Posted by Ramani Sandeep on October 5, 2008

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'");
    }

Posted in ASP.NET, C# 2.0 | Tagged: | 2 Comments »

 
Follow

Get every new post delivered to your Inbox.

Join 317 other followers