private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
int left = e.CellBounds.Left;
int top = e.CellBounds.Top;
int right = e.CellBounds.Right;
int bottom = e.CellBounds.Bottom;
e.Handled = true;
//代䨲码?为a绘?制?列¢D标À¨º题¬a
if (e.RowIndex == -1)
{
for (int i = 0; i < ((DataGridView)sender).ColumnCount; i++)
{
Brush gridBrush = new SolidBrush(Color.Black);
Pen gridLinePen = new Pen(gridBrush);
e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), e.CellBounds); //填¬?充?背À3景¡ã
//绘?制?文?字Á?
// 绘?制?文?字Á?
Brush b1 = new SolidBrush(Color.Red);
if (e.Value != null)
e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,
b1, left + 2,
top + 9, StringFormat.GenericDefault);
e.Graphics.DrawLine(gridLinePen, right - 1,
top - 1, right - 1,
bottom - 1); //绘?制?右®¨°边À?界?
e.Graphics.DrawLine(gridLinePen, left - 1,
bottom - 1, right - 1,
bottom - 1); //绘?制?下?边À?界?
}
}
//下?面?代䨲码?为a绘?制?单Ì£¤元a格?
if (e.RowIndex % 4 == 0 || e.RowIndex % 4 == 1)
{
Brush gridBrush = new SolidBrush(Color.Black);
Pen gridLinePen = new Pen(gridBrush);
if ((e.ColumnIndex == 4 || e.ColumnIndex == 5 || e.ColumnIndex == 7 || e.ColumnIndex == 10) || (e.ColumnIndex == 0 && e.RowIndex % 4 == 0))
{
e.Graphics.FillRectangle(new SolidBrush(Color.LightGray), e.CellBounds); //填¬?充?背À3景¡ã
}
else
e.Graphics.FillRectangle(new SolidBrush(Color.White), e.CellBounds); //填¬?充?背À3景¡ã
//绘?制?文?字Á?
// 绘?制?文?字Á?
Brush b1 = new SolidBrush(Color.Black);
if (e.Value != null)
e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,
b1, left + 2,
top + 1, StringFormat.GenericDefault);
e.Graphics.DrawLine(gridLinePen, right - 1,
top - 1, right - 1,
bottom - 1); //绘?制?右®¨°边À?界?
if (e.ColumnIndex == 0 || e.ColumnIndex == 5)
{ }
else
e.Graphics.DrawLine(gridLinePen, left - 1,
bottom - 1, right - 1,
bottom - 1); //绘?制?下?边À?界?
}
if (e.RowIndex % 4 == 2)
{
Brush gridBrush = new SolidBrush(Color.Black);
Pen gridLinePen = new Pen(gridBrush);
if (e.ColumnIndex == 0 || e.ColumnIndex == 1 || e.ColumnIndex == 9 || e.ColumnIndex == 4 || e.ColumnIndex == 5 || e.ColumnIndex == 7 || e.ColumnIndex == 10)
{
e.Graphics.FillRectangle(new SolidBrush(Color.LightGray), e.CellBounds); //填¬?充?背À3景¡ã
}
else
e.Graphics.FillRectangle(new SolidBrush(Color.White), e.CellBounds);// 绘?制?背À3景¡ã
// 绘?制?文?字Á?
Brush b1 = new SolidBrush(Color.Black);
if (e.Value != null)
e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,
b1, left + 2,
top + 1, StringFormat.GenericDefault);
e.Graphics.DrawLine(gridLinePen, right - 1,
top - 1, right - 1,
bottom - 1); //绘?制?右®¨°边À?界?
if (e.ColumnIndex == 0 || e.ColumnIndex == 5 || e.ColumnIndex == 9 || e.ColumnIndex == 7 || e.ColumnIndex == 10)
{ }
else
e.Graphics.DrawLine(gridLinePen, left - 1,
bottom - 1, right - 1,
bottom - 1); //绘?制?下?边À?界?
}
if (e.RowIndex % 4 == 3)
{
Brush gridBrush = new SolidBrush(Color.Black);
Pen gridLinePen = new Pen(gridBrush);
if (e.ColumnIndex == 0 || e.ColumnIndex == 1 || e.ColumnIndex == 9 || e.ColumnIndex == 4 || e.ColumnIndex == 5 || e.ColumnIndex == 7 || e.ColumnIndex == 10)
{
e.Graphics.FillRectangle(new SolidBrush(Color.LightGray), e.CellBounds); //填¬?充?背À3景¡ã
}
else
e.Graphics.FillRectangle(new SolidBrush(Color.White), e.CellBounds); //绘?制?背À3景¡ã
// 绘?制?文?字Á?
Brush b1 = new SolidBrush(Color.Black);
if (e.Value != null)
e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,
b1, left + 2,
top + 1, StringFormat.GenericDefault);
e.Graphics.DrawLine(gridLinePen, right - 1,
top - 1, right - 1,
bottom - 1); //绘?制?右®¨°边À?界?
e.Graphics.DrawLine(gridLinePen, left - 1,
bottom - 1, right - 1,
bottom - 1); //绘?制?下?边À?界?
}
e.Handled = true;
}