当前位置: 首页 > 编程语言 > C#

如何只绘制DataGridView的单元格背景而不绘制其内容?分享

时间:2023-04-10 16:12:31 C#

如何只绘制DataGridView的单元格背景而不绘制其内容?我只需要绘制DataGridView单元格的背景而不是它的内容。但是当我绘制它时它也会绘制它的内容。请帮我。我的代码是这样的。privatevoidDaywisegrid_CellPainting(objectsender,DataGridViewCellPaintingEventArgse){if(e.RowIndex==0){using(BrushgridBrush=newSolidBrush(this.Daywisegrid.GridColor)){using(BrushbackColorBrush=newSolidBrush(e.CellStyle.Back).)){using(PengridLinePen=newPen(gridBrush)){//清除单元格e.Graphics.FillRectangle(backColorBrush,e.CellBounds);//画底线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;但是如果你想在绘画中这样做,试试这个privatevoidDaywisegrid_CellPainting(objectsender,DataGridViewCellPaintingEventArgse){if(e.RowIndex==0){using(BrushgridBrush=newSolidBrush(this.Daywisegrid.GridColor)){using(BrushbackColorBrush=newSolidBrush(e.CellStyle.BackColor)){using(PengridLinePen=newPen(gridBrush)){//清除单元格e.Graphics.FillRectangle(backColorBrush,e.CellBounds);//画底线e.Graphics.DrawLine(gridLinePen,e.CellBounds.Left,e.CellBounds.Bottom-1,e.CellBounds.Right,e.CellBounds.Bottom-1);//这里你强制绘制内容e.PaintContent(e.ClipBounds);e.Handled=true;}}}}应该是e.Graphics。DrawLine(gridLinePen,e.CellBounds.Left,e.CellBounds.Bottom-1,e.CellBounds.Right-1,e.CellBounds.Bottom-1);e.CellBounds.Right,e.CellBounds.Bottom-1将被下一个单元格删除以上为C#学习教程:如何只绘制DataGridView的单元格背景而不绘制其内容?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: