DataGridView Image Button Column

I want to add an image button to the datagridviews column. I added a datagridview with a datagridviewbuttonColumn but I do not set the image on this. I want to add an image with a button to the datagridviews column and when I click this button, edit or delete the datagridview row. Please how to do this?

+6
source share
5 answers

Although the DataGridView has a ButtonColumn , it does not provide a way to display images directly.

The following link can help you complete your task step by step:

DataGridView Button Cell

Hope this helps ...

+3
source

One way to achieve this is to simply override the Paint method from the DataGridViewButtonCell and call DrawImage from the graphics parameter. The call must be completed after the 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, just create your own DataGridViewButtonColumn and configure the created DeleteCell as the cell template:

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

What is it. Now for the DataGridView, use DeleteColumn:

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

If the result of pressing the button depends on the row of the cell, be sure to process the clicks to the right, that is, to catch the index of the row of the cell.

+5
source

You can use the custom DataGridViewImage class that picks from the DataGridViewImageButtonCell class, as shown below

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

Also check out this thread.

0
source

Just create a datatable with an image column and add an image to it

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

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

Then, on the event event of the GridViewMain_CellContentClick event, write the following code

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

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

0
source

You can provide HTML in the Text-Property asp: ButtonColumn. So you can 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 displayed as follows (HTML):

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

Source: https://habr.com/ru/post/892458/


All Articles