How to add different colour icons/images automatic to the Row Header of a DataGridView based on value in column1?
is it possible to pick up all the colors randomly without manually setting the color based on the value in "COLUMN1" and can also take the value in combination between "COLUMN1" and "COLUMN2"?
Thanks
Private colors As Color()
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
colors = {Color.Red, Color.Green, Color.Orange, Color.Black}
Dim Table1 = New DataTable("TableName")
Table1.Columns.AddRange({
New DataColumn("Column1", GetType(String)),
New DataColumn("Column2", GetType(Integer)),
New DataColumn("Column3", GetType(Integer))
})
Table1.Rows.Add("Item1", 44, 99)
Table1.Rows.Add("Item2", 50, 70)
Table1.Rows.Add("Item3", 75, 85)
Table1.Rows.Add("Item2", 60, 70)
Table1.Rows.Add("Item3", 85, 85)
Table1.Rows.Add("Item4", 77, 21)
Table1.Rows.Add("Item2", 60, 70)
DataGridView1.RowTemplate.Height = 48
DataGridView1.RowHeadersWidth = 48
DataGridView1.DataSource = Table1
End Sub
Private Sub DataGridView1_CellPainting(
sender As Object,
e As DataGridViewCellPaintingEventArgs) _
Handles DataGridView1.CellPainting
If e.RowIndex >= 0 AndAlso
e.ColumnIndex = -1 AndAlso
e.RowIndex <> DataGridView1.NewRowIndex Then
Dim g = e.Graphics
Dim sz = Math.Min(e.CellBounds.Width, e.CellBounds.Height) - 6
Dim ellipseRect = New Rectangle(
e.CellBounds.X + (e.CellBounds.Width - sz) \ 2,
e.CellBounds.Y + (e.CellBounds.Height - sz) \ 2,
sz, sz)
Dim imgRect = Rectangle.Inflate(ellipseRect, -3, -3)
Dim colorIndex = e.RowIndex Mod colors.Length
e.Paint(e.ClipBounds, DataGridViewPaintParts.Background Or
DataGridViewPaintParts.Border Or
DataGridViewPaintParts.SelectionBackground)
Using bmp = My.Resources.SomeImage,
ellipseBrush = New SolidBrush(colors(colorIndex))
g.SmoothingMode = SmoothingMode.AntiAlias
g.FillEllipse(ellipseBrush, ellipseRect)
g.SmoothingMode = SmoothingMode.None
g.DrawImage(bmp, imgRect,
0, 0, bmp.Width, bmp.Height,
GraphicsUnit.Pixel)
End Using
e.Handled = True
End If
End Sub
So, you mean using the same random color for the duplicate rows? For that, you need to group the rows by the cell values and use a color for each group. Doing this in the
CellPaintingevent is a heavy task since it's being raised for each cell in the grid. Hence, I suggest adding a hiddenDataColumnto theDataTableto keep the color index of each row. The indices are set when you bind the control the first time, and when the user modifies the values.If you have a DataBase, fill the
DataTablebefore you add theColorIndexcolumn. To update the DB, you don't need to do anything special regarding the additional column(s). TheINSERTandUPDATEcommands execute their SQLCommandTextqueries and ignore anything else in theColumnscollection. However, you can get a copy of the currentDataTableand exclude the non-database columns.For example:
As another option, use the
DataGridViewRow.Tagproperty to keep the color index. The same could have been written like this.Also, you could create a
Dictionay(Of Integer, Color)where eachKeyValuePairholds the row index and the ellipse color.