Zahra
Zahra

Reputation: 41

Giving a datagridview row a background image in C#.net

I want to set an image as background for a datagridview but I can't find a property to do this.

Upvotes: 0

Views: 5106

Answers (3)

alexb
alexb

Reputation: 971

This should be closer to what you need

Image image;

private void Form1_Load(object sender, EventArgs e)
{
    image = Image.FromFile(@"D:\x.jpg");
}

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
    row.DefaultCellStyle.BackColor = Color.Transparent;
}

private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
    e.Graphics.DrawImage(image, e.RowBounds);
}

Of course you can alternate images depending on your row index. If it not enough let me know.

Upvotes: 1

Ioana O
Ioana O

Reputation: 197

You could give it a CSS Class or modify the theme's row Css Class (if you have one), to have a background property:

.rowClass { background: url('your file here') top left no-repeat; }

Upvotes: 0

x4rf41
x4rf41

Reputation: 5337

you can use the paint event of the datagridview. like this:

    Image bgImage;

    public Form1()
    {
        InitializeComponent();
        bgImage = Image.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\Koala.jpg");
    }

    private void dataGridView1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawImageUnscaled(bgImage, new Point(0, 0));
    }

Upvotes: 0

Related Questions