Solved

Change background color of cell in .Net Winforms DataGrid

Posted on 2006-11-13
6
799 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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