| Bindya 2005-12-02, 9:36 pm |
| I am developing a windows based application in C# which has a datagrid in it
that requires colour coding in just one of its cells. The grid has a red
coloured cell when it loads itself (ie., the colour coding is not
conditional). What i have done now is that i have added a component class to
the application which defines a gridcolumnstyle, in which i have overriden
the paint event and used the above class as the column style for the grid .
But my problem is that the paint event is not getting fired on formload. I
have used a system datagrid. Is there any setting that im missing ??????
All i have done in the form is that i have added a datatable with some
values and assigned those values to the grid and assigned the component class
as the grid style(component class code given below.)
This is the code tht i have written in the component class:
using System;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace datagrid
{
/// <summary>
/// Custom Paint for DataGridTextBoxColumn.
/// </summary>
public class ControlPanelColumnStyle : DataGridTextBoxColumn
{
// public DataGridTextBoxStyledColumn( ICustomGridCellProperties
controller )
// {
// _controller = controller;
// }
private int i = 0;
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)
{
bool DisposeForeBrush = false;
bool DisposeBackBrush = false;
// the idea is to conditionally set the foreBrush and/or backbrush
// depending upon some criteria on the cell value
try
{
object src = source.List[ rowNum ];
Color fg = GetForegroundColor( src );
foreBrush=new SolidBrush( fg );
DisposeForeBrush = true;
Color bg = GetBackgroundColor( src );
backBrush = new SolidBrush( bg );
DisposeBackBrush = true;
}
finally
{
// make sure the base class gets called to do the drawing with
// the possibly changed
base.Paint(g, bounds, source, rowNum, backBrush, foreBrush,
alignToRight);
if ( DisposeForeBrush )
foreBrush.Dispose();
if ( DisposeBackBrush )
backBrush.Dispose();
i++;
}
}
public System.Drawing.Color GetForegroundColor(object row)
{
DataRowView view = (DataRowView)row;
DataRow r = view.Row;
if ( r["col4"] != DBNull.Value )
{
if ( i == 1)
return Color.White;
else
return SystemColors.ControlText;
}
return SystemColors.ControlText;
}
public System.Drawing.Color GetBackgroundColor(object row)
{
DataRowView view = row as DataRowView;
DataRow r = view.Row;
if (r["col4"] != DBNull.Value)
{
if ( i == 1)
return Color.Red;
else
return SystemColors.Window;
}
return SystemColors.Window;
}
}
}
|