7

I want to add image button in datagridviews column . I added the datagridview with datagridviewbuttonColumn but I don't set the image to this . I want to add image with button in datagridviews column and when click this button datagridview row edit or delete. Please How do I do that!

1

5 Answers 5

7

One way this can be achieved is to simply override Paint method from DataGridViewButtonCell and and call DrawImage form the graphics parameter. The call must occur after base call.

public class DeleteCell : DataGridViewButtonCell {
        Image del = Image.FromFile("..\\..\\img\\delete.ico");
        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
            graphics.DrawImage(del, cellBounds);
        }
    }

After that simply create your own DataGridViewButtonColumn, and set created DeleteCell as the cell template:

public class DeleteColumn : DataGridViewButtonColumn {
        public DeleteColumn() {
            this.CellTemplate = new DeleteCell();
            this.Width = 20;
            //set other options here 
        }
    }

That's it. Now use DeleteColumn for your DataGridView:

dgvBookings.Columns.Add(new DeleteColumn());

If the resulting action of button click depends on cell row, make sure to handle the clicks right, that is, to catch the cell row index.

Sign up to request clarification or add additional context in comments.

Comments

3

Though DataGridView has ButtonColumn it does not directly provides way to display images.

The following link may guide you step by step to achieve your task:

DataGridView Image Button Cell

Hope this helps...

1 Comment

I know this answer is ancient but the link is no longer active. This is why generally responses on SO are encouraged to include working examples rather than just links.
0

You can use customize DataGridViewImage class that inhrit from DataGridViewImageButtonCell class as below

public class DataGridViewImageButtonDeleteCell : DataGridViewImageButtonCell
{
    public override void LoadImages()
    {
       // Load them from a resource file, local file, hex string, etc.          
    }
}

And also, check this topic as well

Comments

0

Simply create datatablewith image column and add image to it

dtMain.Columns.Add("ImageColumn", typeof(Image));

dtMain.Rows.Add(Image.FromFile(photopath + "1.jpg"));

Then On event dataGridViewMain_CellContentClick write following code

  if (e.ColumnIndex == dataGridViewMain.Columns["ImageColumn"].Index)
  {
  lblShowCellData.Text = dataGridViewMain.Rows[e.RowIndex].Cells["CustomerName"].Value.ToString();
  // Do some thing else....
  }

Download full code at http://tablegridview.blogspot.in

1 Comment

This is something that I would not encourage to do. Why? Because you add things to the data (!) that do not belong there. You actually modify the data to change the view.
-4

It is possible to provide HTML in the Text-Property of asp:ButtonColumn. So you can actually do something like this:

<asp:ButtonColumn Text="&lt;img src='icons/delete.gif' border='0' title='Delete entry' &gt;" CommandName="delete"></asp:ButtonColumn>

This is rendered as follows (HTML):

<td>
  <a href="...">
    <img src='icons/delete.gif' border='0' title='Delete entry' />
  </a>
</td>

2 Comments

DataGridView is on desktop not asp.net,your answer isn't valid
You are right. I hope it will be helpful for someone using ASP.NET nevertheless. I found this page via Google while looking for a solution using ASP.NET.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.