midgetgem
Sun Nov 27 14:14:05 CST 2005
OK, this is my DataGridColoredTextBoxColumn class:
public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
{
public DataGridColoredTextBoxColumn() : base()
{}
// based on
http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q620q
protected override void Paint(Graphics g, Rectangle bounds,
CurrencyManager source,
int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
{
// the idea is to conditionally set the foreBrush and/or backbrush
// depending upon some criteria on the cell value
// Here, we color 0 as white, 1 as black, 2 as cross
string cellValue = (this.GetColumnValueAtRow(source,
rowNum)).ToString();
switch(cellValue)
{
case "0": foreBrush = new SolidBrush(Color.Black);
g.FillRectangle(backBrush, bounds.X, bounds.Y, bounds.Width,
bounds.Height);
Font font = new Font(FontFamily.GenericSansSerif, (float)8 );
g.DrawString( cellValue, font, Brushes.White, bounds.X,
bounds.Y);
break;
case "1": foreBrush = new SolidBrush(Color.Black);
break;
case "X": foreBrush = new SolidBrush(Color.White);
break;
}
if((rowNum + 1) % 5 == 0)
{
backBrush = Brushes.Red; // any brush you want
}
// the base class gets called to do the drawing with
// the possibly changed brushes
base.Paint(g, bounds, source, rowNum, backBrush, foreBrush,
alignToRight);
}
}
and my form behind code declares it using
private System.Windows.Forms.DataGrid puzzleDataGrid;
private DataGridColoredTextBoxColumn colouredTBCol;
and use it here:
pixelPixGrid = new Grid(path);
DataGridTableStyle tableStyle = new DataGridTableStyle();
DataGridColoredTextBoxColumn colTbCol;
for (int i=0; i <= pixelPixGrid.noOfColsInGrid(); i++) // including
extra column
{
colTbCol = new DataGridColoredTextBoxColumn();
colTbCol.HeaderText =
pixelPixGrid.getGridDataTable().Columns[i].ColumnName;
colTbCol.MappingName =
pixelPixGrid.getGridDataTable().Columns[i].ColumnName;
// Adds our column style to the table style collection
tableStyle.GridColumnStyles.Add(colTbCol);
}
// Now, make the dataGrid use our own tablestyle and bind it to our
table
puzzleDataGrid.TableStyles.Clear();
puzzleDataGrid.TableStyles.Add(tableStyle);
// set puzzle
puzzleDataGrid.DataSource = pixelPixGrid.getGridDataTable();
and that's about it really, it never breaks in my overridden paint
method.
Thanks for your help, and I look forward to your reply!
Gemma