2012-12-23 51 views
1

我想使用c#在windows窗體中自定義我的datagridviews。我想要做的就是用漸變我做出塗料DataGrid標題:Winform C#Datagridview paint header

public void Colorear_Barra_abajo(object sender, PaintEventArgs e) 
    { 
     Rectangle r = new Rectangle(0,0, panel_Borde_abajo.Width, panel_Borde_abajo.Height); 

     if (r.Width > 0 && r.Height > 0) 
     { 
      Color c1 = Color.FromArgb(255, 54, 54, 54); 
      Color c2 = Color.FromArgb(255, 62, 62, 62); 
      Color c3 = Color.FromArgb(255, 98, 98, 98); 

      LinearGradientBrush br = new LinearGradientBrush(r, c1, c3, 90, true); 
      ColorBlend cb = new ColorBlend(); 
      cb.Positions = new[] { 0, (float)0.5, 1 }; 
      cb.Colors = new[] { c1, c2 , c3 }; 
      br.InterpolationColors = cb; 

      // paint 
      e.Graphics.FillRectangle(br, r); 
     } 
    } 

的問題是,datagridviews沒有油漆事件,因此我不能使用此。有沒有辦法用漸變繪製標題?或者唯一的方法是選擇一種背景顏色?

謝謝..

回答

6

如果你的意思Column Header,是的。在CellPainting Event

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
    { 
     if (e.RowIndex == -1) 
     { 
      Color c1 = Color.FromArgb(255, 54, 54, 54); 
      Color c2 = Color.FromArgb(255, 62, 62, 62); 
      Color c3 = Color.FromArgb(255, 98, 98, 98); 

      LinearGradientBrush br = new LinearGradientBrush(e.CellBounds, c1, c3, 90, true); 
      ColorBlend cb = new ColorBlend(); 
      cb.Positions = new[] { 0, (float)0.5, 1 }; 
      cb.Colors = new[] { c1, c2, c3 }; 
      br.InterpolationColors = cb; 


      e.Graphics.FillRectangle(br, e.CellBounds); 
      e.PaintContent(e.ClipBounds); 
      e.Handled = true; 
     } 
    } 
+0

工作!萬分感謝! – Andres