Datagridview-在Datagridview问题c#windows窗体上绘制矩形

Datagridview-在Datagridview问题c#windows窗体上绘制矩形,windows,forms,datagridview,Windows,Forms,Datagridview,我有3个问题: 当我以良好的方式完成任务时 为什么当我滚动dataGridView时,绘制的矩形会消失 为什么绘画这么慢 下面是代码,我想在其中绘制一个彩色矩形,每个列中的单元格组上都有文本,它们具有相同的值,空值不应该具有矩形 void DataGridView1CellPainting(对象发送方,DataGridViewCellPaintingEventArgs e) { foreach(此.dataGridView1.Columns中的DataGridViewColumn列){ Da

我有3个问题:

  • 当我以良好的方式完成任务时

  • 为什么当我滚动dataGridView时,绘制的矩形会消失

  • 为什么绘画这么慢

下面是代码,我想在其中绘制一个彩色矩形,每个列中的单元格组上都有文本,它们具有相同的值,空值不应该具有矩形

void DataGridView1CellPainting(对象发送方,DataGridViewCellPaintingEventArgs e) {

foreach(此.dataGridView1.Columns中的DataGridViewColumn列){


DataGridView1CellPaint事件旨在绘制或更改一个单元格的绘制行为

DGV为每个可见单元格引发此事件

绘制其他单元格时,代码速度会减慢


您找到其他更好的解决方案了吗?我也有类似的问题
            string tempCellValue = string.Empty;
            int tempRectX = -1;
            int tempRectY = -1;
            int tempRectYEnd = -1;
            int tempRectWidth = -1;
            int tempRectHeight = -1;

            foreach (DataGridViewRow row in this.dataGridView1.Rows){

                Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(
                    column.Index, row.Index,true);

                DataGridViewCell cell = dataGridView1.Rows[row.Index].Cells[column.Index];
                if (  cell.Value!=null){


                    if (tempRectX==-1){
                        tempRectX = rect.Location.X;
                        tempRectY = rect.Location.Y;
                        tempCellValue = cell.Value.ToString();
                    }else
                        if (cell.Value.ToString()!=tempCellValue){
                        tempRectYEnd = rect.Location.Y;

                        Rectangle newRect = new Rectangle(tempRectX,
                                                          tempRectY , 5  ,
                                                          tempRectYEnd  );
                        using (
                            Brush gridBrush = new SolidBrush(Color.Coral),
                            backColorBrush = new SolidBrush(Color.Coral))
                        {
                            using (Pen gridLinePen = new Pen(gridBrush))
                            {

                                e.Graphics.FillRectangle(backColorBrush,newRect);

                            }    }
                        tempRectX=-1;
                        tempCellValue = string.Empty;
                    }
                     }else if (tempRectX!=-1){
                    tempRectYEnd = rect.Location.Y;
                    Rectangle newRect = new Rectangle(tempRectX,
                                                      tempRectY , 50  ,
                                                      tempRectYEnd  );
                    using (
                        Brush gridBrush = new SolidBrush(Color.Coral),
                        backColorBrush = new SolidBrush(Color.Coral))
                    {
                        using (Pen gridLinePen = new Pen(gridBrush))
                        {

                            e.Graphics.FillRectangle(backColorBrush,newRect);

                        }    }
                    tempRectX=-1;
                    tempCellValue = string.Empty;
                }
            }}