开发者

DataGridView Image Button Column

开发者 https://www.devze.com 2023-03-19 11:51 出处:网络
I want to add image button in datagridviews column . I added the datagridview with datagridviewbuttonColumn

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 ed开发者_开发问答it or delete. Please How do I do that!


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.


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...


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


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


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号