Share via


Image on button of the DatagridviewButtonColumn???

Question

Wednesday, May 6, 2009 7:29 PM

Hi,
    Can somebody tell me how to show an image on button of the DatagridviewButtoncolumns????

All replies (13)

Friday, May 8, 2009 8:43 AM ✅Answered

Hi abhi0410,

The button in DataGridViewButtonColumn is not real button, also ComboBox column, CheckBox column are not real control in that column. It is drawn by the system with the control appearance and touch feeling. So the button column does not have many property that a real button have.

Here is a solution may be helpful to you.

  1. Well draw an image (a red cross bmp file or something like that). The size of the image should be the same as a cell's bound.

2. Handle the CellPainting event of DataGridView to draw the image onto the button.
Here is the code
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex > -1)
            {
                Image img = Image.FromFile(@"D:\DeleteIcon.bmp");
                e.Graphics.DrawImage(img, e.CellBounds.Location);
                e.PaintContent(e.CellBounds);
                e.Handled = true;
            }
        }

Notice: e.PaintContent will draw the original button to the cell. So that button will have DeleteIcon.bmp on it. It remains the click touch feeling.

Does it meet your need?

Sincerely,
Kira Qian

Please mark the replies as answers if they help and unmark if they don't.


Monday, May 11, 2009 6:49 AM ✅Answered

hey Kira...
I just changed one stuff now....

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex >-1)
            {
                Image img = Properties.Resources.DeleteRule;

**                 e.Graphics.DrawImage(img, e.CellBounds.Location);**
                 **e.PaintContent(e.CellBounds);
                **
                e.Handled = true;

            }
        }

I enter changed the flow of the code and the image appears. i mean i first drew the button and then drew image:

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex >-1)
            {
                Image img = Properties.Resources.DeleteRule;

                 e.PaintContent(e.CellBounds);
**                 e.Graphics.DrawImage(img, e.CellBounds.Location);**
                 **     **
                e.Handled = true;

            }
        }

Now image appear. :-)
 But it appears at the left most corner of the cell and not on the center of the button...


Thursday, May 7, 2009 12:12 AM | 1 vote

Could you use the DataGridViewImageColumn? Or do you need to have it look like a button? I did not see any features of the DataGridViewButton column that seemed to allow assigning an image.www.insteptech.com


Thursday, May 7, 2009 4:45 AM

i wan to make the column look like button only....basically it is going to be delete button...so i thought of placing an image which could show that it is for delete rather then text on the image....

By the way, Can i delete the specified row on the click on the DataGridViewImageColumn's cell?????


Saturday, May 9, 2009 10:49 AM

Hi Kira
         i tried your code and changed couple of stuff but it doesnt work...

ok wait...

The code i wrote after seeing your code is :

  private void dgRuleDesc_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex == 1 && e.RowIndex > -1)
            {
                Image img = Properties.Resources.Delete;
                e.Graphics.DrawImage(img, e.CellBounds.Location);
                e.PaintContent(e.CellBounds);
                e.Handled = true;

            }
        }

// I changed your code here : **Image img = Image.FromFile(@"D:\DeleteIcon.bmp");

i wrote:** Image img = Properties.Resources.Delete;
as my image is in resource....

i did try like  : Image img = Image.FromFile( Properties.Resources.Delete**);**
but it gave error...

So i changed to : Image img = Properties.Resources.Delete;
but the problem is that image doesnt come on top of the button...

i tried with different cell, i  mean different colum which in normal textbox cell...there the image is visible
but when i change the e.ColumeIndex to button colum then the image is not visible...

How to over come that????

i must be doing something wrong becoz of which image doesn't come on top of the button...

Help me out...

 

 


Monday, May 11, 2009 2:06 AM

Hi abhi0410,

I am using Windows 2003 to test the code. It all works properly, I can see the image on the button. What operating system did you use? Could you please made a little project and send it to me(v-kiqian@microsoft.com). I need to test it.

Sincerely,
Kira Qian

Please mark the replies as answers if they help and unmark if they don't.


Monday, May 11, 2009 6:16 AM

Hi kira.
      i will do that right away... and i am using Xp (Service Pack 2).


Monday, May 11, 2009 6:42 AM

Hi Kira,
          I have send the sample project +  i had to remove exe file from the application.Hope it runs on your machine..


Monday, May 11, 2009 7:16 AM

Hi abhi0410,

I have test the project you have sent me. You have done all right. The delete image should be as width and height as the cellbounds with the red cross in the center of it. So it will appear to the center of the cell. To accomplish this. You'd better use PhotoShop to well design a picture.

Please contact me if you have further question.

Sincerely,
Kira Qian

Please mark the replies as answers if they help and unmark if they don't.


Monday, May 11, 2009 7:35 AM

Hi Kira.
    Thanks. I just had to change the flow of your code to make the image appear on the button.. :-)

Will use PhotoShop to desgin picture.

Thanks..

Regards
Abhishek


Monday, May 11, 2009 8:01 AM

Hi Kira,,,

        I have one question..I have posted the question here.
Can you help me there???

The link is :
http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/90b1990e-a206-41f2-8048-e54c675bf2d8

Thanks.


Monday, May 11, 2009 8:09 AM

Hi abhi0410,

That thread is handled by one of my colleague in MSDN team. I am not allow to take over it. Please understand.

If you have any more question, welcome to MSDN.

Sincerely,
Kira QianPlease mark the replies as answers if they help and unmark if they don't.


Monday, May 11, 2009 8:16 AM

Oh ok...
             I was unaware of this rule.
But he didn't respond again so i thought of asking you...Ok i will wait for his reply.

And if it is OK Can i drop a mail to you with the link of thread if i post any query in future???

will this be alright?

Thanks.