Link to home
Start Free TrialLog in
Avatar of zattz
zattz

asked on

Change background color of cell in .Net Winforms DataGrid

Hi

Can somebody show me some example code that changes the color of a specific cell in a .net winforms datagrid?

I want to programatically change the cell color if it's a certain value
Avatar of zattz
zattz

ASKER

I found the following C# code on http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q745q
but I don't know how to translate it to delphi.net

[C#]
 
     public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
 
     {
 
          protected override void Paint(System.Drawing.Graphics g,
 
               System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager
 
               source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush
 
               foreBrush, bool alignToRight)
 
          {
 
          // the idea is to conditionally set the foreBrush and/or backbrush
 
          // depending upon some crireria on the cell value
 
          // Here, we color anything that begins with a letter higher than 'F'
 
               try{
 
                    object o = this.GetColumnValueAtRow(source, rowNum);
 
                    if( o!= null)
 
                    {
 
                         char c = ((string)o)[0];
 
                         if( c > 'F')
 
                         {
 
                         // could be as simple as
 
                         // backBrush = new SolidBrush(Color.Pink);
 
                         // or something fancier...
 
                              backBrush = new LinearGradientBrush(bounds,
 
                                   Color.FromArgb(255, 200, 200),
 
                                   Color.FromArgb(128, 20, 20),
 
                                   LinearGradientMode.BackwardDiagonal);
 
                              foreBrush = new SolidBrush(Color.White);
 
                         }
 
                    }
 
               }
 
                catch(Exception ex){ /* empty catch */ }
 
               finally{
 
                    // make sure the base class gets called to do the drawing with
 
                    // the possibly changed brushes
 
                    base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
 
               }
 
          }
 
     }
Avatar of Eddie Shipman
Personally, I wouldn't use the built-in datagrids for ANYTHING because of their limitations.
Try SourceGrid from here: http://www.devage.com/SourceGrid/SourceGrid_EN.html

Although it is written in C#, you can user th eassmeblies with Delphi.Net.

It is an excellent control with so much functionality.

Oh, BTW, the new version is only .Net2 compatible so download one of the older versions here:
http://www.devage.com/SourceGrid/SourceGridVersions.html
Avatar of zattz

ASKER

For this project I have to use the .NET built in datagrid
ASKER CERTIFIED SOLUTION
Avatar of Mohammed Nasman
Mohammed Nasman
Flag of Palestine, State of image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zattz

ASKER

Thank you so much Mohammed. I really appreciate it.