954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

is it possible to set backgroundimage to selected cell

can anybody tel me ? to do in datagridview at runtime.actualy that s textboxcolumn. whenerver click the cell it should show the background imag. is it possible in C# windows.if u any idea plz help me out.thnks

Renukavani
Junior Poster
123 posts since Jul 2008
Reputation Points: 10
Solved Threads: 23
 

dataGridView1.Rows[Row].Cells[ColName].Style.SelectionBackColor =your color(Color.Red)

for image find this link:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rowprepaint(VS.80).aspx

viswa_007
Newbie Poster
23 posts since Sep 2007
Reputation Points: 10
Solved Threads: 3
 

Yes you can

implement the cell

private void dataGridView1_CellClick(object sender,
DataGridViewCellEventArgs e)
{

dataGridView1.BackgroundImage = Image.FromFile(imagepath);
}

Jugortha
Junior Poster
172 posts since Oct 2007
Reputation Points: 11
Solved Threads: 16
 

Yes you can

implement the cell

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { dataGridView1.BackgroundImage = Image.FromFile(imagepath); }


hi, i want to set backgroundimage to selected cell not grid backgroundimage

Renukavani
Junior Poster
123 posts since Jul 2008
Reputation Points: 10
Solved Threads: 23
 

yes you can, and with a small amount of imagination it was very obvious and very simple. In fact MSDN shows almost how to do it, it shows how you draw something for a given cell and it took me like 2 minutes of my life which I bothered to do.

And you guys wonder why I get narked? Tiny amount of reading of msdn.

where b is a bitmap loaded in or such.

e.PaintContent(e.CellBounds);
            if (dataGridView1.CurrentCell.RowIndex == e.RowIndex && dataGridView1.CurrentCell.ColumnIndex == e.ColumnIndex)
            {
                e.Graphics.DrawImage(b, e.CellBounds);
            }
            else
            {
                e.PaintBackground(e.CellBounds, true);
            }
            e.Handled = true;
LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

hi Lizr, thnks for reply. in which event i have to specify cellclick or in paint or cellpainting event. can u tel me

Renukavani
Junior Poster
123 posts since Jul 2008
Reputation Points: 10
Solved Threads: 23
 

Um

take a look at the events one kinda stands out.

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

yes you can, and with a small amount of imagination it was very obvious and very simple. In fact MSDN shows almost how to do it, it shows how you draw something for a given cell and it took me like 2 minutes of my life which I bothered to do.

And you guys wonder why I get narked? Tiny amount of reading of msdn.

where b is a bitmap loaded in or such.

e.PaintContent(e.CellBounds);
            if (dataGridView1.CurrentCell.RowIndex == e.RowIndex && dataGridView1.CurrentCell.ColumnIndex == e.ColumnIndex)
            {
                e.Graphics.DrawImage(b, e.CellBounds);
            }
            else
            {
                e.PaintBackground(e.CellBounds, true);
            }
            e.Handled = true;

i want to c the content as wel as img. but the image s overlapping the cell content. how to c both .is it possible.plz tel me

Renukavani
Junior Poster
123 posts since Jul 2008
Reputation Points: 10
Solved Threads: 23
 

I just noticed it pasted it funny.

That drawcontents should be just before the handled line not the first. Otherwise yes the background ovewrites it but you would have noticed that..

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

ya the image s overlapping in cellpainting event ,so if i added before the image , im drawing the string means each and every time the cellpainting event s firing whenever mouse enters to the grid.so the problwm s lot of records s available in grid. every time its drawing text based on clicked content.any more suggestion.

Renukavani
Junior Poster
123 posts since Jul 2008
Reputation Points: 10
Solved Threads: 23
 

when you say its overlapping - what exactly is it overlapping? do you even mean overlapping? Please explain more clearly your issue as at the moment its not clear at all. If necessary, draw a picture or take a screen shot or something and use pointers and point out what you're getting you're not expecting.

the code I gave will only draw the picture for the current cell,and do normal behavor for all others, so I dont understand what the rest of your post is on about..

Please explain more fully and also explain what you've done to try and fix it.

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You