Solved

Change background color of cell in .Net Winforms DataGrid

Posted on 2006-11-13
6
772 Views
Last Modified: 2013-11-22
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
0
Comment
Question by:zattz
  • 3
  • 2
6 Comments
 

Author Comment

by:zattz
ID: 17929788
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);
 
               }
 
          }
 
     }
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 17931517
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.

0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 17931535
Oh, BTW, the new version is only .Net2 compatible so download one of the older versions here:
http://www.devage.com/SourceGrid/SourceGridVersions.html
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:zattz
ID: 17932942
For this project I have to use the .NET built in datagrid
0
 
LVL 22

Accepted Solution

by:
mnasman earned 500 total points
ID: 17936719
Hi,
 
  I just made quick conversion,  and used Employees table from northwind db to test it, and worked fine

uses
  System.Drawing.Drawing2D;

//
  type
  DataGridColoredTextBoxColumn = class(DataGridTextBoxColumn)
   protected
    procedure Paint(g: System.Drawing.Graphics; bounds: System.Drawing.Rectangle;
      source: System.Windows.Forms.CurrencyManager; rowNum: Integer; backBrush: System.Drawing.Brush;
      foreBrush: System.Drawing.Brush; alignToRight: Boolean); override;
  end;

// implementation

procedure TWinForm.TWinForm_Load(sender: System.Object; e: System.EventArgs);
var
tableStyle : DataGridTableStyle;
i, numCols : Integer;
aColumnTextColumn :  DataGridColoredTextBoxColumn;
begin
  bdpDataAdapter1.Fill(ds,'Employees');
  tableStyle := DataGridTableStyle.Create;
  tableStyle.MappingName := 'Employees';
  numCols := ds.Tables['Employees'].Columns.Count;

  for I := 0 to numCols - 1 do
  begin
    aColumnTextColumn := DataGridColoredTextBoxColumn.Create;
    aColumnTextColumn.HeaderText := ds.Tables['Employees'].Columns[i].ColumnName;
    aColumnTextColumn.MappingName := ds.Tables['Employees'].Columns[i].ColumnName;
    tableStyle.GridColumnStyles.Add(aColumnTextColumn);
  end;

  dataGrid1.TableStyles.Clear;
  dataGrid1.TableStyles.Add(tableStyle);
  dataGrid1.DataSource := ds.Tables['Employees'];

end;

{ DataGridColoredTextBoxColumn }

procedure DataGridColoredTextBoxColumn.Paint(g: System.Drawing.Graphics; bounds: System.Drawing.Rectangle;
  source: System.Windows.Forms.CurrencyManager; rowNum: Integer; backBrush: System.Drawing.Brush;
  foreBrush: System.Drawing.Brush; alignToRight: Boolean);
var
  c: WideChar;
  o: System.Object;
begin
  try
    try
      o := Self.GetColumnValueAtRow(source, rowNum);
      if (o <> nil) then
      begin
        c := string(o)[1];
        if (c > 'F') then
        begin
          backBrush := LinearGradientBrush.Create(bounds, Color.FromArgb(255,
                200, 200), Color.FromArgb(128, 20, 20), LinearGradientMode.BackwardDiagonal);
          foreBrush := SolidBrush.Create(Color.White);
        end;
      end;
    except
      on ex: Exception do
      ;
    end;
  finally
    inherited Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
  end;
end;


HTH

Best Regards,
Mohammed Nasman
0
 

Author Comment

by:zattz
ID: 17936815
Thank you so much Mohammed. I really appreciate it.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now