Thursday, April 13, 2006

Work Related

Doing Custom DataGridTextBoxColumn in C#.Net

This Column, has a Centered Header, but contains right aligned numeric data. Also the background Colour is changed according to cell value.

private StringFormat m_strFormat = null;

public UCCustomColumn()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
// TODO: Add any initialization after the InitializeComponent call
m_strFormat = new StringFormat();
m_strFormat.Alignment = StringAlignment.Far;
m_strFormat.Trimming = StringTrimming.None;
}

protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
{

SolidBrush BackgroundBrush;
// Get value on current cell
double dValue = (double) this.GetColumnValueAtRow(source, rowNum);
string strValue = dValue.ToString(this.Format);
double dATValue,dUVValue;
if (rowNum ==0)
{
dATValue = (double)this.GetColumnValueAtRow(source, rowNum);
dUVValue = (double)this.GetColumnValueAtRow(source, rowNum+1);
}
else
{
dATValue = (double) this.GetColumnValueAtRow(source, rowNum-1);
dUVValue = (double)this.GetColumnValueAtRow(source, rowNum);
}

SolidBrush StrongBrush = new SolidBrush(Color.FromArgb(51,102,255));
SolidBrush MediumBrush = new SolidBrush(Color.FromArgb(153,204,255));
SolidBrush SoftBrush = new SolidBrush(Color.White);


if((dATValue >25.7) && (dUVValue >=3))
BackgroundBrush = StrongBrush;
else if((dATValue <=25.7) && (dUVValue >=3))
BackgroundBrush = MediumBrush;
else if((dATValue <=25.7) && (dUVValue <3))
BackgroundBrush = SoftBrush;
else
BackgroundBrush = SoftBrush;

g.FillRectangle(BackgroundBrush, bounds);
RectangleF CellBounds = new System.Drawing.RectangleF(bounds.Left, bounds.Top, bounds.Width, bounds.Height);
g.DrawString(strValue, this.DataGridTableStyle.DataGrid.Font, foreBrush, CellBounds, m_strFormat);
}

No comments: