DataGrid change any row color ( C# )
-
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
-
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
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); } }
-
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); } }
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
-
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); } }
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