DataGrid question

  • Thread starter Thread starter toysrwill
  • Start date Start date
T

toysrwill

Is it possible to make a single cell with its contents be in red color
while the rest of the datagrid is of normal color. If so how?
Thanks in advance
Will
 
toysrwill said:
Is it possible to make a single cell with its contents be in red color
while the rest of the datagrid is of normal color. If so how?
Thanks in advance
Will

You can override the DataGridTextBoxColumn and in the OnPaint method you'd
assign red as the foreground. You would need to supply the logic as to which
cells you keep as default and which you change, likely from the cell's
contents.

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

try
{
object o = this.GetColumnValueAtRow(source, rowNum);
if( o!= null)
{
//o is a reference to the cell's contents
//you can modify the backBrush and foreBrush to whatever color
you want
foreBrush = new SolidBrush(Color.Red);
}
}
}
catch(Exception ex)
{
ex.ToString();
}
finally
{
//Paint the string
base.Paint(g, bounds, source, rowNum, backBrush,
foreBrush,alignToRight);
}
}
}
 
Back
Top