DatagridView Custom Cell Styling

Last week, I came across this issue. How to change the style of particular cells.  Being .net 2.0 I thought this would as easy as locating the cell i wanted to change the style, change the background, invalidate the cell and that was it. 

 Well, for my surprise it didnt turn out to be like that.  I kept running tests and verifying that my code was correct, wondering why the cell background didnt change.    It didnt make sense to me, that this apparently easy issue would take longer than expected.

 Well, after some research, I found out there are two ways of doing this correctly. 

– Creating custom cell class and custom column class for the datagridview.

– Implementing a handler for the DataGridViewCellPainting event.

Creating two custom classes just to change the background color or font type of the cell seems like too much overhead. So I will cover the latter method, since is easier to implement. 

 First, we need to understand a couple of things.

– If this event doesn’t get implement the control would follow its normal rendering of the cell. So we always need a condition to distinct the cell we want to customized.  

– Since we need to implement our painting fucntionality, we need to be cautious when writing the text content back to the cell. Check for DBNull values as well as for null values, also ensure to explicitly cast the value of the cell using the Convert function. 


//Condition to locate the cell we want to customized
if (e.RowIndex == 0 && e.ColumnIndex == 1)
{
//String format required to align to the text to the right inside the cell
StringFormat format = new StringFormat(StringFormat.GenericDefault);
format.Alignment = StringAlignment.Far;
format.LineAlignment = StringAlignment.Center;
format.Trimming = StringTrimming.EllipsisCharacter;
Rectangle newRect = new Rectangle(e.CellBounds.X + 1,
e.CellBounds.Y + 1, e.CellBounds.Width - 4,
e.CellBounds.Height - 4);
e.CellStyle.BackColor = Color.LightSkyBlue;
using (
Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
// Erase the cell.
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

// Draw the grid lines (only the right and bottom lines;
// DataGridView takes care of the others).
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
e.CellBounds.Bottom - 1);
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
e.CellBounds.Top, e.CellBounds.Right - 1,
e.CellBounds.Bottom);

// Draw the text content of the cell, right alignment
if (e.Value != null || e.Value != DBNull.Value)
{

e.Graphics.DrawString( Convert.ToString(e.FormattedValue),
e.CellStyle.Font, Brushes.Black, e.CellBounds,
format);
}
e.Handled = true;
}
}

}
So far, this example will just change the background color of the cell. But it is possible to further customized the cell. These changes while interesting are out of the scope of this post.

Happy Blogging!!

~ by ajhorus on May 3, 2007.

3 Responses to “DatagridView Custom Cell Styling”

  1. Hi.
    Good design, who make it?

  2. Very interesting and beautiful site. It is a lot of ful information. Thanks.

  3. Hi,
    Thanks! I did manage to change the cell colours without using the painting thing, but it cleared everything when I clicked the cell header. Do you have any idea how to make the colours persistent? or atleast if there is a format string for the cell colours or something?

    Thanks!

Leave a comment