Datagridview - рисование прямоугольника на проблеме datagridview c # оконных форм - PullRequest
0 голосов
/ 03 июля 2010

У меня 3 вопроса:

  • все ли я хорошо выполняю свою задачу

  • почему при прокрутке dataGridView закрашенные прямоугольники исчезают ..

  • почему живопись такая медленная ...

Вот код, в котором я хочу нарисовать цветной прямоугольник с текстом на группах ячеек в каждом столбце, которые имеют одинаковые значения, пустые значения не должны иметь прямоугольников

void DataGridView1CellPainting (отправитель объекта, DataGridViewCellPaintingEventArgs e) {

foreach (столбец DataGridViewColumn в 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 Ответ

1 голос
/ 05 июля 2010

Событие DataGridView1CellPainting предназначено для рисования или изменения поведения рисования для одной ячейки.

DGV вызывает это событие для каждой видимой ячейки.

Когда рисуете другие ячейки, ваш код замедляется.

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...