' Create a new button column and add it to the DataGridViewPrivate buttonColumn As New DataGridViewButtonColumn()
buttonColumn.HeaderText = "Actions"
buttonColumn.Text = "Edit"
buttonColumn.Name = "EditButton"
buttonColumn.UseColumnTextForButtonValue = True
dataGridView1.Columns.Add(buttonColumn)
' Handle the CellPainting event to add an icon to the cell
private void dataGridView1_CellPainting(Object sender, DataGridViewCellPaintingEventArgs e)
If e.ColumnIndex = dataGridView1.Columns("EditButton").Index AndAlso e.RowIndex >= 0 Then
e.Paint(e.CellBounds, DataGridViewPaintParts.All)
Dim icon = My.Resources.EditIcon ' Replace with your desired icon
Dim iconWidth = icon.Width
Dim iconHeight = icon.Height
Dim cellHeight = e.CellBounds.Height
Dim cellWidth = e.CellBounds.Width
' Calculate the position of the icon and text
Dim iconX = e.CellBounds.Left + (cellWidth - iconWidth) \ 2
Dim iconY = e.CellBounds.Top + (cellHeight - iconHeight) \ 2
Dim textX = e.CellBounds.Left + (cellWidth - e.Graphics.MeasureString("Edit", e.CellStyle.Font).Width) \ 2
Dim textY = e.CellBounds.Top + (cellHeight - e.CellStyle.Font.Height) \ 2
' Draw the icon and text
e.Graphics.DrawImage(icon, New Rectangle(iconX, iconY, iconWidth, iconHeight))
e.Graphics.DrawString("Edit", e.CellStyle.Font, Brushes.Black, New Point(textX, textY))
e.Handled = True
End If