如何仅绘制 DataGridView 的单元格背景而不是其内容? [英] How to paint only DataGridView's cell background not its content?

查看:11
本文介绍了如何仅绘制 DataGridView 的单元格背景而不是其内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需要绘制 DataGridView 单元格的背景而不是它的内容.但是当我在绘制它时,它也绘制它的内容.请帮帮我.

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.

我的代码是这样的.

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 事件来改变单元格背景颜色就这样做


i am not sure why you need to catch CellPainting event to change cell background color just do it like this

Daywisegrid.Rows[RowIndex].Cells[columnIndex].Style.BackColor = Color.Red;

但是如果你想在绘画中做到这一点,试试这个

But if you want to do it in painting try 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);

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

这篇关于如何仅绘制 DataGridView 的单元格背景而不是其内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆