如何根据列1中的值自动向DataGridView的行标题添加不同颜色的图标/图像

huangapple go评论88阅读模式
英文:

How to add different colour icons/images automatic to the Row Header of a DataGridView based on value in column1

问题

如何根据列1中的值自动将不同颜色的图标/图像添加到DataGridView的行标题中?

是否可以在不手动设置基于“列1”中的值的颜色的情况下随机选择所有颜色,还可以考虑“列1”和“列2”之间的值组合?

谢谢

  1. Private colors As Color()
  2. Protected Overrides Sub OnLoad(e As EventArgs)
  3. MyBase.OnLoad(e)
  4. colors = {Color.Red, Color.Green, Color.Orange, Color.Black}
  5. Dim Table1 = New DataTable("TableName")
  6. Table1.Columns.AddRange({
  7. New DataColumn("Column1", GetType(String)),
  8. New DataColumn("Column2", GetType(Integer)),
  9. New DataColumn("Column3", GetType(Integer))
  10. })
  11. Table1.Rows.Add("Item1", 44, 99)
  12. Table1.Rows.Add("Item2", 50, 70)
  13. Table1.Rows.Add("Item3", 75, 85)
  14. Table1.Rows.Add("Item2", 60, 70)
  15. Table1.Rows.Add("Item3", 85, 85)
  16. Table1.Rows.Add("Item4", 77, 21)
  17. Table1.Rows.Add("Item2", 60, 70)
  18. DataGridView1.RowTemplate.Height = 48
  19. DataGridView1.RowHeadersWidth = 48
  20. DataGridView1.DataSource = Table1
  21. End Sub
  22. Private Sub DataGridView1_CellPainting(
  23. sender As Object,
  24. e As DataGridViewCellPaintingEventArgs) _
  25. Handles DataGridView1.CellPainting
  26. If e.RowIndex >= 0 AndAlso
  27. e.ColumnIndex = -1 AndAlso
  28. e.RowIndex <> DataGridView1.NewRowIndex Then
  29. Dim g = e.Graphics
  30. Dim sz = Math.Min(e.CellBounds.Width, e.CellBounds.Height) - 6
  31. Dim ellipseRect = New Rectangle(
  32. e.CellBounds.X + (e.CellBounds.Width - sz) \ 2,
  33. e.CellBounds.Y + (e.CellBounds.Height - sz) \ 2,
  34. sz, sz)
  35. Dim imgRect = Rectangle.Inflate(ellipseRect, -3, -3)
  36. Dim colorIndex = e.RowIndex Mod colors.Length
  37. e.Paint(e.ClipBounds, DataGridViewPaintParts.Background Or
  38. DataGridViewPaintParts.Border Or
  39. DataGridViewPaintParts.SelectionBackground)
  40. Using bmp = My.Resources.SomeImage,
  41. ellipseBrush = New SolidBrush(colors(colorIndex))
  42. g.SmoothingMode = SmoothingMode.AntiAlias
  43. g.FillEllipse(ellipseBrush, ellipseRect)
  44. g.SmoothingMode = SmoothingMode.None
  45. g.DrawImage(bmp, imgRect,
  46. 0, 0, bmp.Width, bmp.Height,
  47. GraphicsUnit.Pixel)
  48. End Using
  49. e.Handled = True
  50. End If
  51. End Sub
英文:

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

  1. Private colors As Color()
  2. Protected Overrides Sub OnLoad(e As EventArgs)
  3. MyBase.OnLoad(e)
  4. colors = {Color.Red, Color.Green, Color.Orange, Color.Black}
  5. Dim Table1 = New DataTable(&quot;TableName&quot;)
  6. Table1.Columns.AddRange({
  7. New DataColumn(&quot;Column1&quot;, GetType(String)),
  8. New DataColumn(&quot;Column2&quot;, GetType(Integer)),
  9. New DataColumn(&quot;Column3&quot;, GetType(Integer))
  10. })
  11. Table1.Rows.Add(&quot;Item1&quot;, 44, 99)
  12. Table1.Rows.Add(&quot;Item2&quot;, 50, 70)
  13. Table1.Rows.Add(&quot;Item3&quot;, 75, 85)
  14. Table1.Rows.Add(&quot;Item2&quot;, 60, 70)
  15. Table1.Rows.Add(&quot;Item3&quot;, 85, 85)
  16. Table1.Rows.Add(&quot;Item4&quot;, 77, 21)
  17. Table1.Rows.Add(&quot;Item2&quot;, 60, 70)
  18. DataGridView1.RowTemplate.Height = 48
  19. DataGridView1.RowHeadersWidth = 48
  20. DataGridView1.DataSource = Table1
  21. End Sub
  22. Private Sub DataGridView1_CellPainting(
  23. sender As Object,
  24. e As DataGridViewCellPaintingEventArgs) _
  25. Handles DataGridView1.CellPainting
  26. If e.RowIndex &gt;= 0 AndAlso
  27. e.ColumnIndex = -1 AndAlso
  28. e.RowIndex &lt;&gt; DataGridView1.NewRowIndex Then
  29. Dim g = e.Graphics
  30. Dim sz = Math.Min(e.CellBounds.Width, e.CellBounds.Height) - 6
  31. Dim ellipseRect = New Rectangle(
  32. e.CellBounds.X + (e.CellBounds.Width - sz) \ 2,
  33. e.CellBounds.Y + (e.CellBounds.Height - sz) \ 2,
  34. sz, sz)
  35. Dim imgRect = Rectangle.Inflate(ellipseRect, -3, -3)
  36. Dim colorIndex = e.RowIndex Mod colors.Length
  37. e.Paint(e.ClipBounds, DataGridViewPaintParts.Background Or
  38. DataGridViewPaintParts.Border Or
  39. DataGridViewPaintParts.SelectionBackground)
  40. Using bmp = My.Resources.SomeImage,
  41. ellipseBrush = New SolidBrush(colors(colorIndex))
  42. g.SmoothingMode = SmoothingMode.AntiAlias
  43. g.FillEllipse(ellipseBrush, ellipseRect)
  44. g.SmoothingMode = SmoothingMode.None
  45. g.DrawImage(bmp, imgRect,
  46. 0, 0, bmp.Width, bmp.Height,
  47. GraphicsUnit.Pixel)
  48. End Using
  49. e.Handled = True
  50. End If
  51. End Sub

答案1

得分: 1

使用相同的随机颜色来标识重复行吗?为了实现这个目标,你需要根据单元格的值对行进行分组,并为每个分组使用一种颜色。在CellPainting事件中执行这个操作是一项繁重的任务,因为它对网格中的每个单元格都会触发。因此,我建议在DataTable中添加一个隐藏的DataColumn,用于保存每行的颜色索引。这些索引在首次绑定控件时设置,以及用户修改值时更新。

  1. ' +
  2. Imports System.Reflection
  3. Private colors As Color()
  4. Private bmp As Bitmap
  5. Sub New()
  6. InitializeComponent()
  7. ' 减少闪烁...
  8. DataGridView1.GetType().
  9. GetProperty("DoubleBuffered",
  10. BindingFlags.Instance Or BindingFlags.NonPublic).
  11. SetValue(DataGridView1, True)
  12. End Sub
  13. Protected Overrides Sub OnLoad(e As EventArgs)
  14. MyBase.OnLoad(e)
  15. ' 收集深色...
  16. colors = GetType(Color).
  17. GetProperties(BindingFlags.Public Or BindingFlags.Static).
  18. Where(Function(pi) pi.PropertyType = GetType(Color)).
  19. Select(Function(pi) CType(pi.GetValue(GetType(Color), Nothing), Color)).
  20. Where(Function(c)
  21. Return ((
  22. c.R * 0.299F +
  23. c.G * 0.587F +
  24. c.B * 0.114F) / 256.0F) <= 0.5F
  25. End Function).ToArray()
  26. bmp = My.Resources.Money
  27. Dim Table1 = New DataTable("TableName")
  28. Table1.Columns.AddRange({
  29. New DataColumn("Column1", GetType(String)),
  30. New DataColumn("Column2", GetType(Integer)),
  31. New DataColumn("Column3", GetType(Integer)),
  32. New DataColumn("ColorIndex", GetType(Integer))
  33. })
  34. Table1.Rows.Add("Item1", 44, 99)
  35. Table1.Rows.Add("Item2", 50, 70)
  36. Table1.Rows.Add("Item3", 75, 85)
  37. Table1.Rows.Add("Item2", 60, 70)
  38. Table1.Rows.Add("Item3", 75, 85)
  39. Table1.Rows.Add("Item4", 77, 21)
  40. Table1.Rows.Add("Item2", 50, 70)
  41. ' ...等等。
  42. DataGridView1.DataSource = Table1
  43. DataGridView1.Columns("ColorIndex").Visible = False
  44. UpdateColorColumn()
  45. Table1.AcceptChanges()
  46. End Sub
  47. Protected Overrides Sub OnFormClosed(e As FormClosedEventArgs)
  48. MyBase.OnFormClosed(e)
  49. bmp.Dispose()
  50. DirectCast(DataGridView1.DataSource, IDisposable)?.Dispose()
  51. End Sub
  52. Private Sub DataGridView1_CellValueChanged(sender As Object,
  53. e As DataGridViewCellEventArgs) _
  54. Handles DataGridView1.CellValueChanged
  55. UpdateColorColumn()
  56. End Sub
  57. Private Sub DataGridView1_CellPainting(sender As Object,
  58. e As DataGridViewCellPaintingEventArgs) _
  59. Handles DataGridView1.CellPainting
  60. If e.RowIndex >= 0 AndAlso e.ColumnIndex = -1 AndAlso
  61. e.RowIndex <> DataGridView1.NewRowIndex Then
  62. Dim ellipseSize = DataGridView1.RowTemplate.Height - 3
  63. Dim ellipseRect = New Rectangle(
  64. e.CellBounds.X + (e.CellBounds.Width - ellipseSize) \ 2,
  65. e.CellBounds.Y + (e.CellBounds.Height - ellipseSize) \ 2,
  66. ellipseSize, ellipseSize)
  67. Dim imgSize = ellipseSize ' 或者更小...
  68. Dim imgRect = New Rectangle(
  69. ellipseRect.X + (ellipseRect.Width - imgSize) \ 2,
  70. ellipseRect.Y + (ellipseRect.Height - imgSize) \ 2,
  71. imgSize, imgSize)
  72. Dim drv = DirectCast(DataGridView1.Rows(e.RowIndex).DataBoundItem, DataRowView)
  73. Dim colorIndex = Convert.ToInt32(drv.Item("ColorIndex"))
  74. Dim g = e.Graphics
  75. Dim gs = g.Save()
  76. e.Paint(e.ClipBounds, DataGridViewPaintParts.Background Or
  77. DataGridViewPaintParts.Border Or
  78. DataGridViewPaintParts.SelectionBackground)
  79. Using ellipseBrush = New SolidBrush(colors(colorIndex))
  80. g.SmoothingMode = SmoothingMode.AntiAlias
  81. g.FillEllipse(ellipseBrush, ellipseRect)
  82. g.InterpolationMode = InterpolationMode.HighQualityBicubic
  83. g.DrawImage(bmp, imgRect, 0, 0, bmp.Width, bmp.Height,
  84. GraphicsUnit.Pixel)
  85. End Using
  86. g.Restore(gs)
  87. e.Handled = True
  88. End If
  89. End Sub
  90. Private Sub UpdateColorColumn()
  91. Dim dt = DirectCast(DataGridView1.DataSource, DataTable)
  92. dt.EndInit()
  93. Dim groups = dt.AsEnumerable().
  94. GroupBy(Function(g) New With {
  95. Key .Col1 = g.Item(0),
  96. Key .Col2 = g.Item(1),
  97. Key .Col3 = g.Item(2) ' 移除此处以便根据前两个分组。
  98. })
  99. For i = 0 To groups.Count - 1
  100. Dim group = groups(i)
  101. Dim colorIndex = i Mod colors.Length
  102. For Each row In group
  103. row("ColorIndex") = colorIndex
  104. Next
  105. Next
  106. End Sub

如果你有一个数据库,请在添加ColorIndex列之前填充DataTable。要更新数据库,你不需要特别处理额外的列。INSERTUPDATE命令会执行它们的SQL CommandText查询,并忽略Columns集合中的其他内容。不过,你可以获取当前DataTable的副本,并排除非数据库列。

例如:

  1. Dim dt = Table1.DefaultView.
  2. ToTable(False, Table1.Columns.
  3. Cast(Of DataColumn).
  4. Where(Function(c) c.ColumnName <> "ColorIndex").
  5. Select(Function(c) c.ColumnName).ToArray())

作为另一种选择,你可以使用DataGridViewRow.Tag属性来保存颜色索引。下面是另一种写法:

  1. Private colors As Color()
  2. ' ...
  3. Protected Overrides Sub OnLoad(e As EventArgs)
  4. MyBase.OnLoad(e)
  5. ' ...
  6. DataGridView1.DataSource = Table1
  7. UpdateColorIndices()
  8. End Sub
  9. ' ...
  10. Private Sub DataGridView1_CellPainting(sender As Object,
  11. e As DataGridViewCellPaintingEventArgs) _
  12. Handles DataGridView1.CellPainting
  13. If e.RowIndex >= 0 AndAlso e.ColumnIndex = -1 AndAlso
  14. e.RowIndex <> DataGridView1.NewRowIndex Then
  15. Dim ellipseSize = DataGridView1.RowTemplate.Height - 3
  16. Dim ellipseRect = New Rectangle(
  17. e.CellBounds.X + (e.CellBounds.Width - ellipseSize) \ 2,
  18. e.CellBounds.Y + (e.CellBounds.Height - ellipseSize) \ 2,
  19. ellipseSize, ellipseSize)
  20. Dim imgSize = ellipseSize
  21. Dim imgRect = New Rectangle(
  22. ellipseRect.X + (ellipseRect.Width - imgSize) \ 2,
  23. ellipseRect.Y
  24. <details>
  25. <summary>英文:</summary>
  26. 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 `CellPainting` event is a heavy task since it&#39;s being raised for each cell in the grid. Hence, I suggest adding a hidden `DataColumn` to the `DataTable` to 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.
  27. ```vb.net
  28. &#39; +
  29. Imports System.Reflection
  30. Private colors As Color()
  31. Private bmp As Bitmap
  32. Sub New()
  33. InitializeComponent()
  34. &#39; To reduce the flickering...
  35. DataGridView1.GetType().
  36. GetProperty(&quot;DoubleBuffered&quot;,
  37. BindingFlags.Instance Or BindingFlags.NonPublic).
  38. SetValue(DataGridView1, True)
  39. End Sub
  40. Protected Overrides Sub OnLoad(e As EventArgs)
  41. MyBase.OnLoad(e)
  42. &#39; Collect dark colors...
  43. colors = GetType(Color).
  44. GetProperties(BindingFlags.Public Or BindingFlags.Static).
  45. Where(Function(pi) pi.PropertyType = GetType(Color)).
  46. Select(Function(pi) CType(pi.GetValue(GetType(Color), Nothing), Color)).
  47. Where(Function(c)
  48. Return ((
  49. c.R * 0.299F +
  50. c.G * 0.587F +
  51. c.B * 0.114F) / 256.0F) &lt;= 0.5F
  52. End Function).ToArray()
  53. bmp = My.Resources.Money
  54. Dim Table1 = New DataTable(&quot;TableName&quot;)
  55. Table1.Columns.AddRange({
  56. New DataColumn(&quot;Column1&quot;, GetType(String)),
  57. New DataColumn(&quot;Column2&quot;, GetType(Integer)),
  58. New DataColumn(&quot;Column3&quot;, GetType(Integer)),
  59. New DataColumn(&quot;ColorIndex&quot;, GetType(Integer))
  60. })
  61. Table1.Rows.Add(&quot;Item1&quot;, 44, 99)
  62. Table1.Rows.Add(&quot;Item2&quot;, 50, 70)
  63. Table1.Rows.Add(&quot;Item3&quot;, 75, 85)
  64. Table1.Rows.Add(&quot;Item2&quot;, 60, 70)
  65. Table1.Rows.Add(&quot;Item3&quot;, 75, 85)
  66. Table1.Rows.Add(&quot;Item4&quot;, 77, 21)
  67. Table1.Rows.Add(&quot;Item2&quot;, 50, 70)
  68. &#39; ...etc.
  69. DataGridView1.DataSource = Table1
  70. DataGridView1.Columns(&quot;ColorIndex&quot;).Visible = False
  71. UpdateColorColumn()
  72. Table1.AcceptChanges()
  73. End Sub
  74. Protected Overrides Sub OnFormClosed(e As FormClosedEventArgs)
  75. MyBase.OnFormClosed(e)
  76. bmp.Dispose()
  77. DirectCast(DataGridView1.DataSource, IDisposable)?.Dispose()
  78. End Sub
  79. Private Sub DataGridView1_CellValueChanged(sender As Object,
  80. e As DataGridViewCellEventArgs) _
  81. Handles DataGridView1.CellValueChanged
  82. UpdateColorColumn()
  83. End Sub
  84. Private Sub DataGridView1_CellPainting(sender As Object,
  85. e As DataGridViewCellPaintingEventArgs) _
  86. Handles DataGridView1.CellPainting
  87. If e.RowIndex &gt;= 0 AndAlso e.ColumnIndex = -1 AndAlso
  88. e.RowIndex &lt;&gt; DataGridView1.NewRowIndex Then
  89. Dim ellipseSize = DataGridView1.RowTemplate.Height - 3
  90. Dim ellipseRect = New Rectangle(
  91. e.CellBounds.X + (e.CellBounds.Width - ellipseSize) \ 2,
  92. e.CellBounds.Y + (e.CellBounds.Height - ellipseSize) \ 2,
  93. ellipseSize, ellipseSize)
  94. Dim imgSize = ellipseSize &#39; Or smaller...
  95. Dim imgRect = New Rectangle(
  96. ellipseRect.X + (ellipseRect.Width - imgSize) \ 2,
  97. ellipseRect.Y + (ellipseRect.Height - imgSize) \ 2,
  98. imgSize, imgSize)
  99. Dim drv = DirectCast(DataGridView1.Rows(e.RowIndex).DataBoundItem, DataRowView)
  100. Dim colorIndex = Convert.ToInt32(drv.Item(&quot;ColorIndex&quot;))
  101. Dim g = e.Graphics
  102. Dim gs = g.Save()
  103. e.Paint(e.ClipBounds, DataGridViewPaintParts.Background Or
  104. DataGridViewPaintParts.Border Or
  105. DataGridViewPaintParts.SelectionBackground)
  106. Using ellipseBrush = New SolidBrush(colors(colorIndex))
  107. g.SmoothingMode = SmoothingMode.AntiAlias
  108. g.FillEllipse(ellipseBrush, ellipseRect)
  109. g.InterpolationMode = InterpolationMode.HighQualityBicubic
  110. g.DrawImage(bmp, imgRect, 0, 0, bmp.Width, bmp.Height,
  111. GraphicsUnit.Pixel)
  112. End Using
  113. g.Restore(gs)
  114. e.Handled = True
  115. End If
  116. End Sub
  117. Private Sub UpdateColorColumn()
  118. Dim dt = DirectCast(DataGridView1.DataSource, DataTable)
  119. dt.EndInit()
  120. Dim groups = dt.AsEnumerable().
  121. GroupBy(Function(g) New With {
  122. Key .Col1 = g.Item(0),
  123. Key .Col2 = g.Item(1),
  124. Key .Col3 = g.Item(2) &#39; Remove this to group by the first two.
  125. })
  126. For i = 0 To groups.Count - 1
  127. Dim group = groups(i)
  128. Dim colorIndex = i Mod colors.Length
  129. For Each row In group
  130. row(&quot;ColorIndex&quot;) = colorIndex
  131. Next
  132. Next
  133. End Sub

If you have a DataBase, fill the DataTable before you add the ColorIndex column. To update the DB, you don't need to do anything special regarding the additional column(s). The INSERT and UPDATE commands execute their SQL CommandText queries and ignore anything else in the Columns collection. However, you can get a copy of the current DataTable and exclude the non-database columns.

For example:

  1. Dim dt = Table1.DefaultView.
  2. ToTable(False, Table1.Columns.
  3. Cast(Of DataColumn).
  4. Where(Function(c) c.ColumnName &lt;&gt; &quot;ColorIndex&quot;).
  5. Select(Function(c) c.ColumnName).ToArray())

As another option, use the DataGridViewRow.Tag property to keep the color index. The same could have been written like this.

  1. Private colors As Color()
  2. &#39; ...
  3. Protected Overrides Sub OnLoad(e As EventArgs)
  4. MyBase.OnLoad(e)
  5. &#39; ...
  6. DataGridView1.DataSource = Table1
  7. UpdateColorIndices()
  8. End Sub
  9. &#39; ...
  10. Private Sub DataGridView1_CellPainting(sender As Object,
  11. e As DataGridViewCellPaintingEventArgs) _
  12. Handles DataGridView1.CellPainting
  13. If e.RowIndex &gt;= 0 AndAlso e.ColumnIndex = -1 AndAlso
  14. e.RowIndex &lt;&gt; DataGridView1.NewRowIndex Then
  15. Dim ellipseSize = DataGridView1.RowTemplate.Height - 3
  16. Dim ellipseRect = New Rectangle(
  17. e.CellBounds.X + (e.CellBounds.Width - ellipseSize) \ 2,
  18. e.CellBounds.Y + (e.CellBounds.Height - ellipseSize) \ 2,
  19. ellipseSize, ellipseSize)
  20. Dim imgSize = ellipseSize
  21. Dim imgRect = New Rectangle(
  22. ellipseRect.X + (ellipseRect.Width - imgSize) \ 2,
  23. ellipseRect.Y + (ellipseRect.Height - imgSize) \ 2,
  24. imgSize, imgSize)
  25. Dim colorIndex = Convert.ToInt32(DataGridView1.Rows(e.RowIndex).Tag)
  26. Dim g = e.Graphics
  27. Dim gs = g.Save()
  28. e.Paint(e.ClipBounds, DataGridViewPaintParts.Background Or
  29. DataGridViewPaintParts.Border Or
  30. DataGridViewPaintParts.SelectionBackground)
  31. Using ellipseBrush = New SolidBrush(colors(colorIndex))
  32. g.SmoothingMode = SmoothingMode.AntiAlias
  33. g.FillEllipse(ellipseBrush, ellipseRect)
  34. g.InterpolationMode = InterpolationMode.HighQualityBicubic
  35. g.DrawImage(bmp, imgRect, 0, 0, bmp.Width, bmp.Height,
  36. GraphicsUnit.Pixel)
  37. End Using
  38. g.Restore(gs)
  39. e.Handled = True
  40. End If
  41. End Sub
  42. Private Sub UpdateColorIndices()
  43. DataGridView1.EndEdit()
  44. Dim groups = DataGridView1.Rows.Cast(Of DataGridViewRow).
  45. Where(Function(r) r.Index &lt;&gt; DataGridView1.NewRowIndex).
  46. GroupBy(Function(r) New With {
  47. Key .Col0 = r.Cells(0).Value,
  48. Key .Col1 = r.Cells(1).Value,
  49. Key .Col2 = r.Cells(2).Value
  50. })
  51. For i = 0 To groups.Count - 1
  52. Dim index = i Mod colors.Length
  53. For Each row In groups(i)
  54. row.Tag = index
  55. Next
  56. Next
  57. DataGridView1.Invalidate()
  58. End Sub

Also, you could create a Dictionay(Of Integer, Color) where each KeyValuePair holds the row index and the ellipse color.

  1. Private colors As Color()
  2. Private ReadOnly dictColors As New Dictionary(Of Integer, Color)
  3. &#39; ...
  4. Private Sub DataGridView1_CellPainting(sender As Object,
  5. e As DataGridViewCellPaintingEventArgs) _
  6. Handles DataGridView1.CellPainting
  7. If e.RowIndex &gt;= 0 AndAlso e.ColumnIndex = -1 AndAlso
  8. e.RowIndex &lt;&gt; DataGridView1.NewRowIndex Then
  9. Dim ellipseSize = DataGridView1.RowTemplate.Height - 3
  10. Dim ellipseRect = New Rectangle(
  11. e.CellBounds.X + (e.CellBounds.Width - ellipseSize) \ 2,
  12. e.CellBounds.Y + (e.CellBounds.Height - ellipseSize) \ 2,
  13. ellipseSize, ellipseSize)
  14. Dim imgSize = ellipseSize
  15. Dim imgRect = New Rectangle(
  16. ellipseRect.X + (ellipseRect.Width - imgSize) \ 2,
  17. ellipseRect.Y + (ellipseRect.Height - imgSize) \ 2,
  18. imgSize, imgSize)
  19. Dim g = e.Graphics
  20. Dim gs = g.Save()
  21. e.Paint(e.ClipBounds, DataGridViewPaintParts.Background Or
  22. DataGridViewPaintParts.Border Or
  23. DataGridViewPaintParts.SelectionBackground)
  24. Using ellipseBrush = New SolidBrush(dictColors(e.RowIndex))
  25. g.SmoothingMode = SmoothingMode.AntiAlias
  26. g.FillEllipse(ellipseBrush, ellipseRect)
  27. g.InterpolationMode = InterpolationMode.HighQualityBicubic
  28. g.DrawImage(bmp, imgRect, 0, 0, bmp.Width, bmp.Height,
  29. GraphicsUnit.Pixel)
  30. End Using
  31. g.Restore(gs)
  32. e.Handled = True
  33. End If
  34. End Sub
  35. Private Sub UpdateColorIndices()
  36. DataGridView1.EndEdit()
  37. dictColors.Clear()
  38. Dim groups = DataGridView1.Rows.Cast(Of DataGridViewRow).
  39. Where(Function(r) r.Index &lt;&gt; DataGridView1.NewRowIndex).
  40. GroupBy(Function(r) New With {
  41. Key .Col0 = r.Cells(0).Value,
  42. Key .Col1 = r.Cells(1).Value,
  43. Key .Col2 = r.Cells(2).Value
  44. })
  45. For i = 0 To groups.Count - 1
  46. Dim index = i Mod colors.Length
  47. For Each row In groups(i)
  48. dictColors(row.Index) = colors(index)
  49. Next
  50. Next
  51. DataGridView1.Invalidate()
  52. End Sub

huangapple
  • 本文由 发表于 2023年6月29日 00:27:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575084.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定