DataGridView Image Button Cell

mond007

Active member
Joined
Apr 26, 2010
Messages
37
Location
near Solihull, Birmingham UK
Programming Experience
10+
Hi
Does anyone know how to programme up a ?Delete Image Button? using an Icon for a DataGridView in Visual Studio .Net

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

I have tried looking exhaustively in many forums and can find #c examples but not .NET. I am unable to get this to work.

VB.NET:
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.
 
I would suggest creating your own column and cell types to encapsulate the functionality. Here's one someone else created earlier:

DataGridView Image Button Cell

Try this for performing the code conversion from C# to VB:

C# to VB Converter

If you have any further issues, post back about those issues specifically.
 
Problem solved. See

Display image button in a Datagridview - vb.Net

DataGridViewButton_Solved.jpg

VB.NET:
        '----------PLACE IN THE FORM LOAD --------
        Dim bc As New DataGridViewButtonColumn
        bc.Tag = False
        bc.Text = "Delete"
        bc.Name = "Delete"
        bc.Width = 20
        DataGridView1.Columns.Add(bc)
        ActiveControl = DataGridView1
        '------------------------------------------------------

    Private Sub DataGridView1_CellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
        If DataGridView1.Columns(e.ColumnIndex).Name = "Delete" AndAlso e.RowIndex >= 0 Then
            e.Paint(e.CellBounds, DataGridViewPaintParts.All)
            e.Graphics.DrawImage(My.Resources.Delete, CInt((e.CellBounds.Width / 2) - (My.Resources.Delete.Width / 2)) + e.CellBounds.X, CInt((e.CellBounds.Height / 2) - (My.Resources.Delete.Height / 2)) + e.CellBounds.Y)
            e.Handled = True
        End If
    End Sub

    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        Dim row As DataGridViewRow
        row = Me.DataGridView1.Rows(e.RowIndex)
        'MsgBox("Current Selected Row is : " & e.RowIndex)

        '-----------------------------------------------------------
        '     DELETE BUTTON CLICKED 
        '-----------------------------------------------------------
        'MsgBox("Clicked Column is " & e.ColumnIndex)
        If e.ColumnIndex = 7 Then

            Dim DeleteQuestionNo As String = DataGridView1.Rows(e.RowIndex).Cells(1).Value

            '  MsgBox("Selected Row is " & DeleteQuestionNo)
            If e.RowIndex <> -1 Then
                If DialogResult.Yes = MessageBox.Show("Please confirm you wish to delete Question No " & DeleteQuestionNo & " ?", "Delete Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) Then
                    DataGridView1.Rows.RemoveAt(e.RowIndex)
                    DataGridView1.Refresh()
                    Call clearPreviewBox()
                End If
            End If
        End If

Kind Regards Kuldip
 

Latest posts

Back
Top