Vb.net DataGridViewButton列图标

Vb.net DataGridViewButton列图标,vb.net,Vb.net,我正在使用vb.net 2.0,我想在DataGridViewButtonColumn中设置一个图标或图像。我知道这个班没有成员。任何人都有想法吗?您可以尝试使用DataGridViewImageColumn添加将事件附加到网格的CellContentClick事件(在事件处理过程中,只有来自列/列(即图像)的事件)最佳答案是来自 但和往常一样,你可以在谷歌上搜索vb.Net,然后用C#获得答案。我在developerfusionwebsite上使用C#to vb.net工具来帮助我转换到vb.

我正在使用vb.net 2.0,我想在DataGridViewButtonColumn中设置一个图标或图像。我知道这个班没有成员。任何人都有想法吗?

您可以尝试使用DataGridViewImageColumn添加将事件附加到网格的CellContentClick事件(在事件处理过程中,只有来自列/列(即图像)的事件)

最佳答案是来自

但和往常一样,你可以在谷歌上搜索vb.Net,然后用C#获得答案。我在developerfusionwebsite上使用C#to vb.net工具来帮助我转换到vb.net,但通常会改变相当多的内容

一旦你有了指针,这个就很容易了

dataGridView\u CellPainting
事件中添加如下内容

Private Sub InvoiceLinesDataGridView_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles InvoiceLinesDataGridView.CellPainting

    If e.ColumnIndex = 3 AndAlso e.RowIndex >= 0 Then
        e.Paint(e.CellBounds, DataGridViewPaintParts.All)

        Dim bmpFind As Bitmap = My.Resources.binoc16_h1
        Dim ico As Icon = Icon.FromHandle(bmpFind.GetHicon)
        e.Graphics.DrawIcon(ico, e.CellBounds.Left + 3, e.CellBounds.Top + 3)
        e.Handled = True
    End If
End Sub
只是为了解释一下,我想使用一个资源,它是一个位图,所以我也把它转换成一个图标。这对我来说非常有效,我得到了一个带有图像的合适的按钮列。列索引有点粗糙,因为这可能会发生变化,所以我希望使用列名来引用它-应该不会太难,但你明白了。与我所看到的其他选项相比,创建扩展自定义列类型要容易得多


这真的应该只是原始控制的一部分,我无法理解为什么微软会如此严重地破坏网格。我一直在尝试使用第三方控件,如Telerik,但原始控件似乎更稳定,因此我现在看看是否可以坚持使用普通控件,并在需要时添加扩展。

我创建了一个可以在CellPaint datagridview事件中调用的方法

  Public Shared Sub SetImageToDataGridViewButtonColumn_CallInCellPaintingEvent(ByRef img As Bitmap, e As System.Windows.Forms.DataGridViewCellPaintingEventArgs)
        e.Paint(e.CellBounds, DataGridViewPaintParts.All & (DataGridViewPaintParts.ContentBackground) & (DataGridViewPaintParts.ContentForeground))
        Dim destRect As Rectangle = New Rectangle(e.CellBounds.X + (e.CellBounds.Width - img.Width) / 2, e.CellBounds.Y + (e.CellBounds.Height - img.Height) / 2, img.Width, img.Height)
        Dim srcRect As Rectangle = New Rectangle(0, 0, img.Width, img.Height)
        e.Graphics.DrawImage(img, destRect, srcRect, GraphicsUnit.Pixel)
        e.Handled = True
  End Sub
在CellPaint事件中,调用此方法,在按钮和e上传递所需的图像。确保在需要的列上使用某种条件进行设置,本例中If指定列0。还请注意,我在我的资源中有我的图像。参考资料:

Private Sub dgv_CellPainting(sender As Object, e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles dgv.CellPainting
    If (e.ColumnIndex = 0 And e.RowIndex >= 0) Then
        SetImageToDataGridViewButtonColumn_CallInCellPaintingEvent(My.Resources.myImg, e)
    End If
End Sub
提示:我发现16x16 png非常适合我使用。您可以使用