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. General Programming
  3. C#
  4. DataGrid change any row color ( C# )

DataGrid change any row color ( C# )

Scheduled Pinned Locked Moved C#
csharpasp-nethelptutorial
4 Posts 3 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.
  • D Offline
    D Offline
    dannygilbert3
    wrote on last edited by
    #1

    Hi ! I found many snippets (samples) of code on the web but every one is for C# with ASP.NET or written in VB.NET. My application is not WEB based but a normal Windows Form with a dataGrid (windows application). Sometime I want to change the background and foreground color of a row depending of some condition (NOT the SELECTED row). I understand the idea of using the databinding event (which is supposed to be throw at each Rows.Add()) but I still have problem with my code. Maybe I don`t really know how to use event. The other idea is to modify the Paint function (I am not very confident with this). Can somebody show me a sample !!! :sigh: Thank you very much ! Danny Gilbert Montréal, Canada

    V 1 Reply Last reply
    0
    • D dannygilbert3

      Hi ! I found many snippets (samples) of code on the web but every one is for C# with ASP.NET or written in VB.NET. My application is not WEB based but a normal Windows Form with a dataGrid (windows application). Sometime I want to change the background and foreground color of a row depending of some condition (NOT the SELECTED row). I understand the idea of using the databinding event (which is supposed to be throw at each Rows.Add()) but I still have problem with my code. Maybe I don`t really know how to use event. The other idea is to modify the Paint function (I am not very confident with this). Can somebody show me a sample !!! :sigh: Thank you very much ! Danny Gilbert Montréal, Canada

      V Offline
      V Offline
      VPMahank
      wrote on last edited by
      #2

      Hai, You can actually inherit from DataGridColumnStyle and override the paint method where you check the condition and set the backbrush color accordingly. Just make sure all of your column styles has this code so the each column in row has same color. Hope this helps Thanks, VPMahank Here is a sample public class DataGridTextBoxColumnCustom : DataGridTextBoxColumn { /// /// Invalidates the display of this column, causing it to be repainted. /// public void Repaint() { Invalidate(); } protected SolidBrush _BackBrush = new SolidBrush(Color.White); protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight) { Brush bkBr = backBrush; try { // you condition check logic if( your condition == true) { backBrush = new SolidBrush(some color); } else if () backBrush = new SolidBrush(some color); else backBrush = new SolidBrush(some color); } catch (Exception exc) { } // ignore finally { base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight); } }

      D C 2 Replies Last reply
      0
      • V VPMahank

        Hai, You can actually inherit from DataGridColumnStyle and override the paint method where you check the condition and set the backbrush color accordingly. Just make sure all of your column styles has this code so the each column in row has same color. Hope this helps Thanks, VPMahank Here is a sample public class DataGridTextBoxColumnCustom : DataGridTextBoxColumn { /// /// Invalidates the display of this column, causing it to be repainted. /// public void Repaint() { Invalidate(); } protected SolidBrush _BackBrush = new SolidBrush(Color.White); protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight) { Brush bkBr = backBrush; try { // you condition check logic if( your condition == true) { backBrush = new SolidBrush(some color); } else if () backBrush = new SolidBrush(some color); else backBrush = new SolidBrush(some color); } catch (Exception exc) { } // ignore finally { base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight); } }

        D Offline
        D Offline
        dannygilbert3
        wrote on last edited by
        #3

        Thank you ! Now I understand more things. I do something like your proposal for any control... overriding the paint method for any control. Thanks again !:-D Danny Gilbert Montréal, Canada

        1 Reply Last reply
        0
        • V VPMahank

          Hai, You can actually inherit from DataGridColumnStyle and override the paint method where you check the condition and set the backbrush color accordingly. Just make sure all of your column styles has this code so the each column in row has same color. Hope this helps Thanks, VPMahank Here is a sample public class DataGridTextBoxColumnCustom : DataGridTextBoxColumn { /// /// Invalidates the display of this column, causing it to be repainted. /// public void Repaint() { Invalidate(); } protected SolidBrush _BackBrush = new SolidBrush(Color.White); protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight) { Brush bkBr = backBrush; try { // you condition check logic if( your condition == true) { backBrush = new SolidBrush(some color); } else if () backBrush = new SolidBrush(some color); else backBrush = new SolidBrush(some color); } catch (Exception exc) { } // ignore finally { base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight); } }

          C Offline
          C Offline
          Cesa37
          wrote on last edited by
          #4

          Using this example I can set a row to a specific color based on its content, but it doesn't work if I sort the datagrid or use a DataView.RowFilter. Does anyone know how to make it work? To clarify, in the paint method I get the number of the row of the datatable, so if row #2 has a value that sets the background to red, row #2 in the datagrid becomes red. But if I use a RowFilter so that row #1 of the datatable isn't shown in the datagrid, it is still row #2 of the datagrid that is red even though the content has changed and the row that should be red now is row #2. /Cesa

          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