zattz
asked on
Change background color of cell in .Net Winforms DataGrid
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
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
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.
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.
Oh, BTW, the new version is only .Net2 compatible so download one of the older versions here:
http://www.devage.com/SourceGrid/SourceGridVersions.html
http://www.devage.com/SourceGrid/SourceGridVersions.html
ASKER
For this project I have to use the .NET built in datagrid
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you so much Mohammed. I really appreciate it.
ASKER
but I don't know how to translate it to delphi.net
[C#]
public class DataGridColoredTextBoxColu
{
protected override void Paint(System.Drawing.Graph
System.Drawing.Rectangle bounds, System.Windows.Forms.Curre
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(s
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.Backwar
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);
}
}
}