I want to change a particular row color of a gridview based on some condition. I am using ASP.NET with C#.
Changing the color of row of the GridView on the basis of particular cell value in ASP.NET C#. Suppose we have a GridView and we want to change the color of the row based on particular cell value of the GridView . To achieve this we need to write code of only few lines on the RowDataBound event of the GridView.
The RowDataBound event is raised when a data row (represented by a GridViewRow object) is bound to data in the GridView control. This enables you to provide an event-handling method that performs a custom routine, such as modifying the values of the data bound to the row, whenever this event occurs.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { e.Row.Attributes.Add("style", "cursor:help;"); if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Alternate) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='orange'"); e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#E56E94'"); e.Row.BackColor = Color.FromName("#E56E94"); } } else { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='orange'"); e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='gray'"); e.Row.BackColor = Color.FromName("gray"); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With