加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何仅绘制DataGridView的单元格背景而不是其内容?

发布时间:2020-12-15 08:31:10 所属栏目:百科 来源:网络整理
导读:我只需要绘制DataGridView单元格的背景而不是它的Content.But,而我正在绘画时它也会绘制其内容.请帮助我. 我的代码是这样的. private void Daywisegrid_CellPainting(object sender,DataGridViewCellPaintingEventArgs e) { if (e.RowIndex == 0 ) { using (
我只需要绘制DataGridView单元格的背景而不是它的Content.But,而我正在绘画时它也会绘制其内容.请帮助我.

我的代码是这样的.

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,e.CellBounds.Bottom-1);

                              // here you force paint of content
                             e.PaintContent( e.ClipBounds  );
                            e.Handled = true;
                        }
                    }
                }
            }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读