DataGridView 图像按钮单元格 - vb.Net

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25183776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:59:37  来源:igfitidea点击:

DataGridView Image Button Cell - vb.Net

.netvb.netdatagridviewimagebutton

提问by mond007

Does anyone know how to programme up a “Delete Image Button” using an Icon for a DataGridView in Visual Studio .Net

有谁知道如何在 Visual Studio .Net 中使用 DataGridView 的图标编写“删除图像按钮”

I am struggling find trying to get my last column to have a delete icon instead of the word "Delete" and I want to display a little dustbin icon.

我很难找到试图让我的最后一列有一个删除图标而不是“删除”这个词,我想显示一个小垃圾箱图标。

I have tried looking in that many places and I am unable to get this to work.

我试过在很多地方寻找,但我无法让它发挥作用。

Private Sub dataGridView1_CellPainting(ByVal sender As Object, ByVal e As 

    DataGridViewCellPaintingEventArgs)
        If e.ColumnIndex = 1 AndAlso e.RowIndex >= 0 Then
            e.Paint(e.CellBounds, DataGridViewPaintParts.All)
        Dim img As Image = Image.FromFile("C:\icon_delete.jpg")
            e.Graphics.DrawImage(img, e.CellBounds.Left + 10, e.CellBounds.Top + 5, 10, 10)
            e.Handled = True
        End If
    End Sub 

Would appreciate some help if anyone knows. Thanks in Advance. Kuldip.

如果有人知道,将不胜感激。提前致谢。库尔迪普。

回答by tezzo

You can use a DataGridViewImageColumnand DataGridView.CellFormattingevent.

您可以使用DataGridViewImageColumnDataGridView.CellFormatting事件。

Private Sub YourDataGridView_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvBilancioAttivita.CellFormatting

    If e.ColumnIndex = -1 Or e.RowIndex = -1 Then Exit Sub

    If YourDataGridView.Columns(e.ColumnIndex).Name = "YourDeleteColumn" Then
        e.Value = Image.FromFile("C:\icon_delete.jpg")
    End If

End Sub