[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Change background color of cell in .Net Winforms DataGrid

Posted on 2006-11-13
6
Medium Priority
?
828 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

656 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