DataGridView图像按钮列

我想在datagridviews列中添加图像按钮。 我使用datagridviewbuttonColumn添加了datagridview,但是我没有将图像设置为this。 我想在datagridviews列中添加带按钮的图像,当单击此按钮时,datagridview行编辑或删除。 请问我该怎么做!

虽然DataGridView具有ButtonColumn但它并不直接提供显示图像的方法。

以下链接可指导您逐步完成任务:

DataGridView图像按钮单元格

希望这可以帮助https://stackoverflow.com/questions/6645699/datagridview-image-button-column/…

可以实现的一种方法是简单地从DataGridViewButtonCell重写Paint方法,并从图形参数调用DrawImage。 呼叫必须基本呼叫之后发生。

 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); } } 

之后,只需创建自己的DataGridViewButtonColumn,并将创建的DeleteCell设置为单元格模板:

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

而已。 现在使用DeleteColumn为您的DataGridView:

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

如果按钮单击的结果操作取决于单元格行,请确保正确处理单击,即捕获单元格行索引。

您可以使用自定义DataGridViewImageButtonCell类的自定义DataGridViewImage类,如下所示

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

此外,还要检查此主题

只需创建带有图像列的datatable并向其添加图像

dtMain.Columns.Add(“ImageColumn”,typeof(Image));

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

然后在事件dataGridViewMain_CellContentClick上编写以下代码

  if (e.ColumnIndex == dataGridViewMain.Columns["ImageColumn"].Index) { lblShowCellData.Text = dataGridViewMain.Rows[e.RowIndex].Cells["CustomerName"].Value.ToString(); // Do some thing elsehttps://stackoverflow.com/questions/6645699/datagridview-image-button-column/.... } 

从http://tablegridview.blogspot.in下载完整代码

可以在asp:ButtonColumn的Text-Property中提供HTML。 所以你实际上可以这样做:

  

这呈现如下(HTML):