Changing the row color dinamically by using a delegate function that depends on row number

Changing the row color dinamically by using a delegate function that depends on row number

Post by SammyBa » Wed, 01 Feb 2006 03:36:25


his is a helper class that enables to display the row back and fore color
on a per row basis. The color is specified by a delegate function that takes
the row number as a parameter. This is a small modification of the class
DataGridEvenOddRows published by Alex Feinman here:

http://www.opennetcf.org/Forums/topic.asp?TOPIC_ID=350



using System;

using System.Drawing;

using System.Collections;

using System.Windows.Forms;

using System.Data;

using System.Reflection;

namespace Conteo

{

/// <summary>

/// Summary description for DataGridEvenOddHandler.

/// </summary>

class DataGridRowColorHandler : IDisposable

{

public delegate Colors GetRowColor(int iRow);

private GetRowColor fGetRowColor;

private DataGrid m_grid;

private object m_r;

//private Color[] arrClr = new Color[] { Color.Cyan, Color.LightYellow };

//private Color[] arrClrText = new Color[] { Color.Black, Color.Black };

private SolidBrush brCell, brCellFore;

private FieldInfo m_fiRowVisibleFirst, m_fiRowVisibleLast,
m_fiColVisibleFirst, m_fiColVisibleLast;

private FieldInfo m_fiRowDrawFirst, m_fiRowDrawLast, m_fiColDrawFirst,
m_fiColDrawLast;

private FieldInfo m_fiRows;

private MethodInfo m_miDrawCells;

private DataGridCell currentCell;

private int iRowVisibleFirst, iRowVisibleLast, iColVisibleFirst,
iColVisibleLast;

private int RowIndex = -1;

public DataGridRowColorHandler(DataGrid grid, GetRowColor
aGetRowColorDelegateFunction)

{

m_grid = grid;

fGetRowColor = aGetRowColorDelegateFunction;

HookGrid();

}

private void HookGrid()

{

m_r = typeof(DataGrid).GetField("m_renderer", BindingFlags.NonPublic |
BindingFlags.GetField | BindingFlags.Instance).GetValue(m_grid);

m_fiRowDrawFirst = m_r.GetType().GetField("m_irowDrawFirst",
BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance);

m_fiRowDrawLast = m_r.GetType().GetField("m_irowDrawLast",
BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance);

m_fiColDrawFirst = m_r.GetType().GetField("m_icolDrawFirst",
BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance);

m_fiColDrawLast = m_r.GetType().GetField("m_icolDrawLast",
BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance);

m_fiRowVisibleFirst = typeof(DataGrid).GetField("m_irowVisibleFirst",
BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance);

m_fiRowVisibleLast = typeof(DataGrid).GetField("m_irowVisibleLast",
BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance);

m_fiColVisibleFirst = typeof(DataGrid).GetField("m_icolVisibleFirst",
BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance);

m_fiColVisibleLast = typeof(DataGrid).GetField("m_icolVisibleLast",
BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance);

m_fiRows = typeof(DataGrid).GetField("m_rlrow", BindingFlags.NonPublic |
BindingFlags.GetField | BindingFlags.Instance);

m_miDrawCells = m_r.GetType().GetMethod("_DrawCells", BindingFlags.NonPublic
| BindingFlags.Instance);

brCell = (SolidBrush)m_r.GetType().GetField("m_brushCellBack",
BindingFlags.NonPublic | BindingFlags.GetField |
BindingFlags.Instance).GetValue(m_r);

brCellFore = (SolidBrush)m_r.GetType().GetField("m_brushCellFore",
BindingFlags.NonPublic | BindingFlags.GetField |
BindingFlags.Instance).GetValue(m_r);

//currentCell = new D