[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Change background color of cell in .Net Winforms DataGrid

Posted on 2006-11-13
6
Medium Priority
?
840 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:Eddie Shipman
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:Eddie Shipman
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

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

Accepted Solution

by:
Mohammed Nasman earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month17 days, 23 hours left to enroll

831 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