0

有没有办法用 HatchPattern i C# 填充 DataGridView 单元格?

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

4

1 回答 1

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);
}

在此处输入图像描述

请注意,我将影线颜色设为半透明以提高内容的可读性。我还检查了其中一个是否用于选中的单元格..

于 2017-03-10T16:03:10.250 回答