user307852
user307852

Reputation: 21

Datagridview - drawing rectangle on datagridview problem c# windows forms

I have 3 questions:

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;
                }
            }}

Upvotes: 0

Views: 4146

Answers (1)

x77
x77

Reputation: 737

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

Upvotes: 1

Related Questions