我只需要绘制 DataGridView 单元格的背景而不是它的内容。但是当我在绘制它时它也会绘制它的内容。请帮帮我。
我的代码是这样的。
private void Daywisegrid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex == 0 )
{
using (Brush gridBrush = new SolidBrush(this.Daywisegrid.GridColor))
{
using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
// Clear cell
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
//Bottom line drawing
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom-1 , e.CellBounds.Right, e.CellBounds.Bottom-1);
e.Handled = true;
}
}
}
}
最佳答案
我不确定为什么你需要捕捉 CellPainting 事件来改变单元格背景颜色,就像这样
Daywisegrid.Rows[RowIndex].Cells[columnIndex].Style.BackColor = Color.Red;
但是如果你想在绘画中做到这一点,试试这个
private void Daywisegrid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex == 0 )
{
using (Brush gridBrush = new SolidBrush(this.Daywisegrid.GridColor))
{
using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
// Clear cell
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
//Bottom line drawing
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom-1 , e.CellBounds.Right, e.CellBounds.Bottom-1);
// here you force paint of content
e.PaintContent( e.ClipBounds );
e.Handled = true;
}
}
}
}
关于c# - 如何只绘制 DataGridView 单元格背景而不是其内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6041044/