public Form1()
{
this.DataGridView.CellPainting += dataGridView1_CellPainting;
}
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex < 0 || e.RowIndex >= this.DataGridView.Rows.Count - 1 || e.ColumnIndex == -1)
{
return;
}
if (e.ColumnIndex == 13&&e.RowIndex<4 )
{
SolidBrush backBrush = new SolidBrush(e.CellStyle.BackColor);
if (this.DataGridView.Rows[e.RowIndex].Selected)
{
backBrush.Color = e.CellStyle.SelectionBackColor;
}
DataGridViewCell preCell = this.DataGridView[e.ColumnIndex - 1, e.RowIndex];
Rectangle re = new Rectangle(e.CellBounds.Left -this.DataGridView.Columns[e.ColumnIndex - 1].Width
, e.CellBounds.Top,
e.CellBounds.Width + this.DataGridView.Columns[e.ColumnIndex - 1].Width
, e.CellBounds.Height);
e.Graphics.FillRectangle(backBrush, re);
Pen pen = new Pen(this.DataGridView.BackgroundColor, 1);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
e.Graphics.DrawLine(pen, re.X, re.Y + re.Height - 1, re.X + re.Width, re.Y + re.Height - 1);
e.Graphics.DrawLine(pen, re.X + re.Width - 1, re.Y, re.X + re.Width - 1, re.Y + re.Height);
if (this.DataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value != null)
{
SizeF strSize = e.Graphics.MeasureString(this.DataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value.ToString(), this.DataGridView.Font);
e.Graphics.DrawString(this.DataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value.ToString(), this.DataGridView.Font, Brushes.Black, re.X + (re.Width - strSize.Width) / 2,
re.Y + (re.Height - strSize.Height) / 2);
e.Handled = true;
}
else
{
}
}
}