Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to change cell text colour of dynamically populated gridview from database in C#

How to change cell text colour of dynamically populated gridview from database in C#

Scheduled Pinned Locked Moved ASP.NET
csharpcssdatabasetutorial
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mansi Arora
    wrote on last edited by
    #1

    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.

    L 1 Reply Last reply
    0
    • M Mansi Arora

      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.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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....

      M 1 Reply Last reply
      0
      • L Lost User

        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....

        M Offline
        M Offline
        Mansi Arora
        wrote on last edited by
        #3

        Its not working I have tried, moreover I need C# code. Please Help

        L 1 Reply Last reply
        0
        • M Mansi Arora

          Its not working I have tried, moreover I need C# code. Please Help

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Can you give me your code what you have written? HTH

          Jinal Desai - LIVE Experience is mother of sage....

          M 1 Reply Last reply
          0
          • L Lost User

            Can you give me your code what you have written? HTH

            Jinal Desai - LIVE Experience is mother of sage....

            M Offline
            M Offline
            Mansi Arora
            wrote on last edited by
            #5

            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.

            L 1 Reply Last reply
            0
            • M Mansi Arora

              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.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              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....

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups