Fill DataGridView cell with Hatch Pattern

972 Views Asked by At

Is there any way to fill DataGridView cell with HatchPattern i C#?

I tried using dataGridView1.Rows[n].Cells[m].Style, but this way i can only change backcolor of the cell.

1

There are 1 best solutions below

0
On

You need to owner-draw the cell, i.e. code the CellPainting event..

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

Note that I made the hatch colors semi-transparent for better readability of the content. I also check for just one of them for the cell being selected..