-
windows datagrid
hi,
I am trying to set a different colour to a cell which is in the 2nd column in a windows datagrid depending on the value of the cell.
(i.e. to check the date in the cell and change the colour of the cell id the date is less than today)
This is a sample code that I have found on the internet.
But the sample is for another purpose. It is testing for any characters higher than 'F'
I don't seem to be able to change it for my purpose.
Can you have a look pls?
Thanks
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);
}
}
}
-
Re: windows datagrid
That code is casting the value as a string, then getting the first character and comparing it to 'F'. You need to cast the object as a DateTime instead and compare it to DateTime.Today. It's exactly the same principle. If the value returned by GetColumnValueAtRow is actually a DateTime then it is OK to cast. If it's actually a string though, you'd need to convert it using DateTime.Parse instead.