2017-03-10 37 views
0

是否有任何方式填充DataGridView单元格与HatchPattern我C#?用填充图案填充DataGridView单元格

我试过使用dataGridView1.Rows [n] .Cells [m] .Style,但是这样我只能改变单元格的背景颜色。

+0

你是什么意思_HatchPattern_?不清楚你在问什么 – Pikoh

+0

使用由两种颜色和HatchStyle制成的HatchBrush对象填充单元格。它通常用于填充形状。 – Szczepan

+0

我不明白你为什么要这样做,但看到[这个问题](http://stackoverflow.com/q/6041044/579895) – Pikoh

回答

1

您需要所有者绘制细胞,即代码CellPainting事件..

using System.Drawing.Drawing2D; 
.. 
private void dataGridView1_CellPainting(object sender, 
             DataGridViewCellPaintingEventArgs e) 
{ 
    Rectangle cr = e.CellBounds; 
    e.PaintBackground(cr, true); 

    Color c1 = Color.FromArgb(64, Color.GreenYellow); 
    Color c2 = Color.FromArgb(64, e.State == DataGridViewElementStates.Selected ? 
       Color.HotPink : Color.RosyBrown) ; 

    if (e.RowIndex == 2 && (e.ColumnIndex >= 3)) 
     using (HatchBrush brush = new HatchBrush(HatchStyle.LargeConfetti, c1, c2)) 
     { 
      e.Graphics.FillRectangle(brush, cr); 
     } 
    e.PaintContent(cr); 
} 

enter image description here

注意,我做了孵化颜色半透明的内容,更好的可读性。我也检查其中一个细胞被选中..