2010-07-02 7 views
0

У меня есть 3 вопроса:DataGridView - рисунок прямоугольник на проблемные DataGridView C# Windows Forms

  • Wheter Я делаю свою задачу в хорошем смысле

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

  • почему картина так медленно ...

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

void DataGridView1CellPainting (object sender, DataGridViewCellPaintingEventArgs e) {

Еогеасп (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; 
       } 
      }} 
+0

Вы нашли другое лучшее решение? У меня схожая проблема – Saint

ответ

1

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

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

При окрашивании других ячеек ваш код замедляется.

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

Смежные вопросы