How to change cell text colour of dynamically populated gridview from database in C#
-
I have populated a dynamic grid view from database with Cell text as ON and OFF. I want that off text should appear in "Red" Colour. Grid rows are generated as no.of employees and columns are days in a particular month from database.
-
I have populated a dynamic grid view from database with Cell text as ON and OFF. I want that off text should appear in "Red" Colour. Grid rows are generated as no.of employees and columns are days in a particular month from database.
Using RowDataBound is frequently the best way. Here's a quick example that changes the row color based on the status of a CheckBoxField.
Protected Sub GridView1\_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) If (e.Row.RowType = DataControlRowType.DataRow) Then Dim cb As CheckBox = CType(e.Row.Cells(1).Controls(0), CheckBox) If (cb.Checked = False) Then e.Row.BackColor = Drawing.Color.LightYellow e.Row.Cells(0).ForeColor = Drawing.Color.Red Else e.Row.ForeColor = Drawing.Color.Blue cb.Enabled = True End If End If End Sub
Please do not repost the question. You have already asked your question here[^] You can write comment if you have any doubt or further query. HTH
Jinal Desai - LIVE Experience is mother of sage....
-
Using RowDataBound is frequently the best way. Here's a quick example that changes the row color based on the status of a CheckBoxField.
Protected Sub GridView1\_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) If (e.Row.RowType = DataControlRowType.DataRow) Then Dim cb As CheckBox = CType(e.Row.Cells(1).Controls(0), CheckBox) If (cb.Checked = False) Then e.Row.BackColor = Drawing.Color.LightYellow e.Row.Cells(0).ForeColor = Drawing.Color.Red Else e.Row.ForeColor = Drawing.Color.Blue cb.Enabled = True End If End If End Sub
Please do not repost the question. You have already asked your question here[^] You can write comment if you have any doubt or further query. HTH
Jinal Desai - LIVE Experience is mother of sage....
Its not working I have tried, moreover I need C# code. Please Help
-
Its not working I have tried, moreover I need C# code. Please Help
-
Can you give me your code what you have written? HTH
Jinal Desai - LIVE Experience is mother of sage....
protected void gvEmpCal_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { CheckBox ckh = e.Row.Cells[0].Controls.Add(0); if (ckh.Checked = false) { e.Row.BackColor = System.Drawing.Color.LightYellow; e.Row.Cells[0].ForeColor = System.Drawing.Color.Red; } } } Actually I need that gridview cells that contains "off" should appear in red colour. Presently On and Off are in same colour.
-
protected void gvEmpCal_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { CheckBox ckh = e.Row.Cells[0].Controls.Add(0); if (ckh.Checked = false) { e.Row.BackColor = System.Drawing.Color.LightYellow; e.Row.Cells[0].ForeColor = System.Drawing.Color.Red; } } } Actually I need that gridview cells that contains "off" should appear in red colour. Presently On and Off are in same colour.
Ok, I got the problem. I think you have not understand the code I have posted. Do one thing. Replace your
CheckBox ckh = e.Row.Cells[0].Controls.Add(0);
with this one
CheckBox ckh = e.Row.Cells[0].Controls[0];
HTH
Jinal Desai - LIVE Experience is mother of sage....