Solved

Change background color of cell in .Net Winforms DataGrid

Posted on 2006-11-13
6
803 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
Technology Partners: 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

Technology Partners: 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!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

687 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