0
votes

I have 3 questions:

  • wheter I am doing my task in a good way

  • why when I scroll dataGridView, painted rectangles dissapear..

  • why painting is so slow...

Here is the code in which I want to draw a colorful rectangle with text on groups of cells in each column, that have the same values, empty values shouldn't have rectangles

void DataGridView1CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {

foreach (DataGridViewColumn column in this.dataGridView1.Columns){

            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;
                }
            }}
1
Did you find any other better solution? I have similar problemSaint

1 Answers

1
votes

The DataGridView1CellPainting event is intended to Paint or change Paint behaviour for one cell.

DGV raises this event for each visible Cell.

When Paint other cells, your code slow down.

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcellpaintingeventargs.aspx