Hi Techies,
I have written code in windows C# based application to color/highlight an individual cell of datagrid depending upon its value eg. For each row if value in Column1 and column2 is not equal then highlight that cell with red color.
I have overridden Paint in a derived column style and set the back color there.
Its working fine but whenever I click on column header for sorting color do not change accordingly. Cell color of datagrid doesn’t change along with sorting; I hope you are getting what is my problem. Urgent help needed.
Here is sample code :
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 criteria on the cell value
try
{
DataTable dtPaint;
DataRow drPaint;
dtPaint = ((DataView)
this.DataGridTableStyle.DataGrid.DataSource).Table;
drPaint = dtPaint.Rows[rowNum];
//If Amount Mismatch then highlight
if (drPaint["Amount"].ToString() != drPaint["Last Amount"].ToString())
{
backBrush =
new LinearGradientBrush(bounds,
Color.IndianRed,
Color.IndianRed,
LinearGradientMode.BackwardDiagonal);
foreBrush =
new SolidBrush(Color.White);
}
}
catch(Exception ex)
{ ex.ToString(); }
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);
}
}
}
Thank you.
Regards,
Nilesh Jagdale