Here is my cell painting method
DataGridView grid = (DataGridView)sender;
if (e.RowIndex == -1 || e.ColumnIndex == -1) { return; }
if ((grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null)) return;
Brush gridBrush = new SolidBrush(GridList[0].GridColor),backColorBrush = new SolidBrush(e.CellStyle.BackColor);
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, ignoring alignment.
if (e.Value != null)
{
Brush brush = new SolidBrush(Color.Red);
Brush brush1 = new SolidBrush(Color.Black);
String s = (String)e.Value;
System.Drawing.Rectangle rect = e.CellBounds;
List<int> pos = null;
if (grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag!=null){
pos = (List<int>)grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag;
}
String[] arr = s.Split('\n');
SizeF stringSize = TextRenderer.MeasureText(e.Graphics, arr[0], e.CellStyle.Font, e.CellBounds.Size);
float wid = stringSize.Height;
int X,Y;
for(int i=0;i<arr.Length;i++){
if (pos==null||pos.IndexOf(i)==-1)
{
X = (e.CellBounds.X);
Y = (e.CellBounds.Y + i * ((int)stringSize.Height));
TextRenderer.DrawText(e.Graphics, arr[i], e.CellStyle.Font, new Point(X, Y), SystemColors.ControlText);
//e.Graphics.DrawString(arr[i], e.CellStyle.Font, brush1, new PointF(X, Y), StringFormat.GenericDefault);
}
else
{
X = (e.CellBounds.X);
Y = (e.CellBounds.Y + i * (int)stringSize.Height );
Brush brushForBox = new SolidBrush(Color.FromArgb(100, 120, 50,0));
e.Graphics.FillRectangle(brushForBox, X, Y, e.CellBounds.Width, (int)stringSize.Height);
TextRenderer.DrawText(e.Graphics, arr[i], e.CellStyle.Font, new Point(X, Y), SystemColors.ControlText);
//e.Graphics.DrawString(arr[i], e.CellStyle.Font, brush, new PointF(X, Y), StringFormat.GenericDefault);
}
}
}
//grid.InvalidateCell(-1, e.RowIndex);
e.Handled = true;
now it works just fine but the text overflows for first and last cells. For example if if first cell in the dataGridView is partially visible then text is rendered in row header. Similarly for last cell in the row text flows out of it. Any suggestion/solution is appreciated.