110,571
社区成员
发帖
与我相关
我的任务
分享
private void dataGridView2_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {
Graphics g = e.Graphics;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
if (e.RowIndex == -1) {
g.FillRectangle(Brushes.White, e.CellBounds);
if (e.ColumnIndex > 0 && e.ColumnIndex < 3) {
Rectangle rect = new Rectangle(//这个合并的区域应该有简洁的办法获取出来的
dataGridView2.RowHeadersWidth + dataGridView2.Columns[0].Width + 1, 1,
dataGridView2.Columns[1].Width, dataGridView2.ColumnHeadersHeight);
rect.Width += dataGridView2.Columns[2].Width;
g.DrawRectangle(Pens.Red, rect.Left, rect.Top, rect.Width - 1, rect.Height - 1);
g.DrawString("dd", this.Font, Brushes.Black, rect, sf);
} else {
g.DrawRectangle(Pens.Red,
e.CellBounds.Left, e.CellBounds.Top,
e.CellBounds.Width - 1, e.CellBounds.Height - 1);
e.PaintContent(e.CellBounds);//绘制文本
}
e.Handled = true;
}
}