2011-05-18 41 views
5

我需要繪製DataGridView單元格的背景,而不是它的Content.But,而我正在繪畫時也繪製它的內容。請幫助我。如何僅繪製DataGridView的單元格背景而不是其內容?

我的代碼是這樣的。

private void Daywisegrid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
     { 
      if (e.RowIndex == 0) 

      { 
       using (Brush gridBrush = new SolidBrush(this.Daywisegrid.GridColor)) 
       { 
        using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor)) 
        { 
         using (Pen gridLinePen = new Pen(gridBrush)) 
         { 
          // Clear cell 
          e.Graphics.FillRectangle(backColorBrush, e.CellBounds); 
          //Bottom line drawing 
          e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom-1 , e.CellBounds.Right, e.CellBounds.Bottom-1); 


          e.Handled = true; 
         } 
        } 
       } 
      } 

回答

8


我不知道爲什麼你需要捕捉CellPainting事件更改單元格背景顏色只是不喜歡這樣

Daywisegrid.Rows[RowIndex].Cells[columnIndex].Style.BackColor = Color.Red; 

但是,如果你想這樣做在繪畫嘗試這個

private void Daywisegrid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
     { 
      if (e.RowIndex == 0) 

      { 
       using (Brush gridBrush = new SolidBrush(this.Daywisegrid.GridColor)) 
       { 
        using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor)) 
        { 
         using (Pen gridLinePen = new Pen(gridBrush)) 
         { 
          // Clear cell 
          e.Graphics.FillRectangle(backColorBrush, e.CellBounds); 
          //Bottom line drawing 
          e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom-1 , e.CellBounds.Right, e.CellBounds.Bottom-1); 

           // here you force paint of content 
          e.PaintContent(e.ClipBounds ); 
          e.Handled = true; 
         } 
        } 
       } 
      } 
0

應該

e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom-1 , e.CellBounds.Right - 1, e.CellBounds.Bottom - 1); 

e.CellBounds.Right, e.CellBounds.Bottom - 1點將被下一個單元擦除。

相關問題