how to change row background color in gridview
Sneha Panda
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Aqua;
if (e.Row.RowType = DataControlRowType.DataRow) {//Check your condition here, Cells[1] for ex. is DONE/Not Done column If(e.Row.Cells[1].Text == "DONE"){e.Row.BackColor = Drawing.Color.Green // This will make row back color green} }
In data bound event if (e.Row.RowType = DataControlRowType.DataRow) {e.Row.BackColor = System.Drawing.Color.Orange; }
You can change row background color in OnRowDataBound event of Gridview. Steps: 1.create OnRowDataBound event. protected void grd_OnRowDataBound(object sender, GridViewRowEventArgs e) {if (e.Row.RowType == DataControlRowType.DataRow){ //do something } } 2. Find particular row which you want change the background color of Grid. String dept=e.Row.Cells[1].Text ; 3. Write the condition for checking the value. Example is there is Department column, check the value is IT or not. If(dept=="IT") 4. If value is OT then assign background color for that row. Ex:- e.Row.BackgroundColor=System.Drawing.Colo}
protected void gvReviewAnswer_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) {string User_Answer = (string)DataBinder.Eval(e.Row.DataItem, "User_Answer"); string CorrectAnswer = (string)DataBinder.Eval(e.Row.DataItem, "CorrectAnswer"); if (User_Answer == "0000") { e.Row.BackColor = System.Drawing.Color.PaleGoldenrod;} else if (User_Answer == CorrectAnswer) { e.Row.BackColor = System.Drawing.Color.PaleGreen;} else if (User_Answer != CorrectAnswer) { e.Row.BackColor = System.Drawing.Color.Pink;}}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e) {if (e.Row.RowType == DataControlRowType.DataRow){int quantity = int.Parse(e.Row.Cells[1].Text);foreach (TableCell cell in e.Row.Cells){if (quantity == 0){cell.BackColor = Color.Red;}if (quantity > 0 && quantity <= 50){cell.BackColor = Color.Yellow;}if (quantity > 50 && quantity <= 100){cell.BackColor = Color.Orange;}}} }
gridviewid.rows[e.selectindex].backcolor=system.drawing.color.desiredcolor
http://www.aspsnippets.com/Articles/Dynamically-change-GridView-Row-Background-Color-based-on-condition-in-ASPNet-using-C-and-VBNet.aspx
By using we can call RowDataBound Event.or else we can assign directly in aspx page.
refer my article http://www.c-sharpcorner.com/UploadFile/0c1bb2/highlighting-particular-row-of-gridview-in-specific-conditio/
Create GridView1_RowDataBound event for your GridView. if (e.Row.RowType == DataControlRowType.DataRow) {//Check your condition here//Get Id from here and based on Id check value in the //underlying dataSource Row where you have "DONE" column value// e.g.// (gridview.DataSource as DataTable), now you can find your row and cell // of "Done"If(Condition True){e.Row.BackColor = Drawing.Color.Red; // your color settings } }
protected void gvReviewAnswer_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) {string User_Answer = (string)DataBinder.Eval(e.Row.DataItem, "User_Answer");string CorrectAnswer = (string)DataBinder.Eval(e.Row.DataItem, "CorrectAnswer");if (User_Answer == "0000") {e.Row.BackColor = System.Drawing.Color.PaleGoldenrod;} else if (User_Answer == CorrectAnswer) {e.Row.BackColor = System.Drawing.Color.PaleGreen;} else if (User_Answer != CorrectAnswer) {e.Row.BackColor = System.Drawing.Color.Pink;}}
use the css background-color=#aabbcc;
We can change by using javascript function and direct command also1. document.getElementById(row).style.backgroundColor = "#ffffda"; GridView1_RowDataBound evente.Row.Attributes.Add("onclick", "javascript:ChangeRowColor('" & e.Row.ClientID & "')");2. Create GridView1_RowDataBound event for your GridView. if (e.Row.RowType == DataControlRowType.DataRow) {If(Condition True){e.Row.BackColor = Drawing.Color.Red; } }foreach (TableCell cell in e.Row.Cells){if (D_inshed >= 6){cell.BackColor = System.Drawing.Color.Orange;}}