Tuesday 15 May 2012

What is difference between RowCreated & RowDataBound events?


Yes, Gridview RowCreated is fired before then RowDataBound Event. If we apply any styles to Gridview Controls we have to choose RowDataBound Event.

But the signature of the RowCreated and RowDataBound events has same signature so everyone doing some confusion which event i need to choose.


  protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='Yellow'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='White'");
        }
                                         // Not Preferred
   
    }

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='Silver'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='White'");
        }
                             // Good Programming
    }

Note: If we apply style to apply in RowCreated and RowDataBound evnts. the styles in RowDataBound event is applied why because RowCreated styles are override by RowDataBound event. as well if don't written any style in RowDataBound event then RowCreated Event Style are applicable.


No comments:

Post a Comment