2010-07-02 48 views
0

我有3个问题:的DataGridView - 对datagridview的问题的C#Windows绘制矩形形成

  • wheter我做我的工作的一个好办法

  • 为什么当我滚动的dataGridView,画矩形dissapear。 。

  • 为什么画是如此之慢...

这里是其中我要绘制与每列中的细胞群中,具有相同的值的文本丰富多彩矩形的代码,空值不应该有矩形

空隙DataGridView1CellPainting(对象发件人,DataGridViewCellPaintingEventArgs E) {

的foreach(在this.dataGridView1.Columns的DataGridViewColumn列){

  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 2012-04-09 09:50:01

回答