如何仅绘制 DataGridView 的单元格背景而不绘制其内容?
我只需要绘制 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;
}
}
}
}
i need to paint only background of DataGridView cell not its Content.But while i'm doing painting it paint its content too.Please help me out.
My code goes like this.
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;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道为什么你需要捕捉 CellPainting 事件来改变单元格背景颜色就这样做
但是如果你想在绘画中这样做尝试这个
i am not sure why you need to catch CellPainting event to change cell background color just do it like this
But if you want to do it in painting try this
应该是
e.CellBounds.Right,e.CellBounds.Bottom - 1 点将被下一个单元格擦除。
Should be
e.CellBounds.Right, e.CellBounds.Bottom - 1
point would be erased by next cell.