DataGridView单元格重绘后在选中时背景色的问题
DataGridView单元格重绘后在选中时背景色为白色,给人的视觉是未能选中,请问如何设置选中时的单元格背景色。我的重绘代码如下。private void grid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex != -1 && e.ColumnIndex == 2)
{
Brush gridBrush = new SolidBrush(this.grid.GridColor);
using (Pen gridLinePen = new Pen(gridBrush))
{
e.Graphics.FillRectangle(new SolidBrush(Color.White), e.CellBounds);
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);
if (e.Value != null)
{
e.Graphics.DrawString((string)e.Value, e.CellStyle.Font, new SolidBrush(Color.Black), e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault);
}
}
e.Handled = true;
}
} 单元格重绘后选中时背景色的问题 --------------------编程问答-------------------- grid_CellPainting 判断是哪行被选中了,
e.Graphics.FillRectangle(new SolidBrush(Color.White), e.CellBounds); 这句设置显示的颜色就好了 --------------------编程问答-------------------- 加上一句,如果当前单元格选中,绘制另一个颜色 --------------------编程问答-------------------- 重新修改一下代码如下,背景能设置颜色了,但它把单元格的内容给遮挡了。代码及效果如下
private void grid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex != -1 && e.ColumnIndex == 2)
{
Brush gridBrush = new SolidBrush(this.grid.GridColor);
Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor);
using (Pen gridLinePen = new Pen(gridBrush))
{
e.Graphics.FillRectangle(new SolidBrush(Color.White), e.CellBounds);
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);
if (e.Value != null)
{
e.Graphics.DrawString((string)e.Value, e.CellStyle.Font, new SolidBrush(Color.Black), e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault);
}
}
if (grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected)
{
backColorBrush = new SolidBrush(Color.Blue);//设置选中的背景色
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
}
e.Handled = true;
}
}
补充:.NET技术 , C#