How to add different colour icons/images PNG Transparent to DataGridView row header in VB.NET?
Is it possible to provide a different color each row in the header row of DataGridView or is there another solution with DataGridView Column Image or else. is there any other solution just fill a different color only because my image file is Transparent png ?
Private Table1 As DataTable
Private Sub createdatatable()
'Dim Table1 As DataTable
Table1 = New DataTable("TableName")
Dim column1 As DataColumn = New DataColumn("Column1")
column1.DataType = System.Type.GetType("System.String")
Dim column2 As DataColumn = New DataColumn("Column2")
column2.DataType = System.Type.GetType("System.Int32")
Dim column3 As DataColumn = New DataColumn("Column3")
column3.DataType = System.Type.GetType("System.Int32")
Table1.Columns.Add(column1)
Table1.Columns.Add(column2)
Table1.Columns.Add(column3)
Dim Row1 As DataRow
Dim Row2 As DataRow
Dim Row3 As DataRow
Row1 = Table1.NewRow()
Row2 = Table1.NewRow()
Row3 = Table1.NewRow()
Row1.Item("Column1") = "Item1"
Row1.Item("Column2") = 44
Row1.Item("Column3") = 99
Row2.Item("Column1") = "Item2"
Row2.Item("Column2") = 50
Row2.Item("Column3") = 70
Row3.Item("Column1") = "Item3"
Row3.Item("Column2") = 75
Row3.Item("Column3") = 85
Table1.Rows.Add(Row1)
Table1.Rows.Add(Row2)
Table1.Rows.Add(Row3)
' Repeat for other rows
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
createdatatable()
DataGridView1.DataSource = Table1
End Sub
Private Sub DataGridView1_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
'Convert the image to icon, in order to load it in the row header column
Dim myBitmap As New Bitmap(My.Resources.money)
Dim myIcon As Icon = Icon.FromHandle(myBitmap.GetHicon())
Dim graphics As Graphics = e.Graphics
'Set Image dimension - User's choice
Dim iconHeight As Integer = 14
Dim iconWidth As Integer = 14
'Set x/y position - As the center of the RowHeaderCell
'INSTANT VB WARNING: Instant VB cannot determine whether both operands of this division are integer types - if they are then you should use the VB integer division operator:
Dim xPosition As Integer = e.RowBounds.X + (DataGridView1.RowHeadersWidth / 2)
Dim yPosition As Integer = e.RowBounds.Y + ((DataGridView1.Rows(e.RowIndex).Height - iconHeight) \ 2)
Dim rectangle As New Rectangle(xPosition, yPosition, iconWidth, iconHeight)
graphics.DrawIcon(myIcon, rectangle)
End Sub
The desired result of each image's different colors
Consider implementing the
CellPainting
event instead to control which paint parts to paint before drawing anything else. The parts defined in theDataGridViewPaintParts
enum.Override the
Onload
method or implement theLoad
event to create the data source, bind the control, and create a color array.In the
CellPainting
event, call thee.Paint(...)
method and select which parts the base should paint, pick a color from the array to create aSolidBrush
used to fill an ellipse as a background of your image. Lastly, draw the image within the ellipse rectangle.For example: