如何在 VB.NET 中突出显示 DataGridView 上的前三个数据
How to Highlight Top Three Data on DataGridView in VB.NET
初学者问题。因此,我在 VB.Net 上有一个项目,其中我 需要突出显示 DataGridView 中的前三行 (按最高值)。我该怎么做?
在这个程序中,我没有使用数据库来存储值,因为所有内容都是生成的,显示在 DataGridView 中,然后突出显示(如我所料)。
规则(伪代码)
If (column="WASPAS")>6.69 then
If(row>3) Then Highlight 3 Rows
ElseIf(row<3) Then Highlight the Rest of Rows
End If
我现在的代码:
For Each row As DataGridViewRow In dgvWASPAS.Rows
If row.Cells(9).Value > 0.69 Then
row.DefaultCellStyle.ForeColor = Color.White
row.DefaultCellStyle.BackColor = Color.Green
End If
Next
我的数据类型是双精度。在这里,我附上我的 DataGridView 看起来像
DataGridView
请帮助我,我被困在这里几天了。感谢您的帮助。
下面创建一个dictionary
,将网格中每一行的行索引和value
添加到dictionary
,然后代码得到一个排序版本的字典排序降序 value
,然后为前三行着色。
下面的代码使用了...
Dim dictionary As Dictionary(Of Integer, Double)
Dictionary
,存储网格行索引和单元格值。该代码将遍历网格中的每一行,并将每一行的索引和值作为一个项目存储在 dictionary.
中
填充 dictionary
后,代码将创建一个新的 LINQ 可枚举,通过降序排序(排序)项目...
Dim sorted = From keyValuePair In dictionary
Order By keyValuePair.Value Descending
显然,排序后,最大值将是 sorted
集合中的第一项。然后下一个等等……完整的例子可能看起来像……
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dictionary As Dictionary(Of Integer, Double) = New Dictionary(Of Integer, Double)()
For Each row As DataGridViewRow In dgvWASPAS.Rows
If row.Cells(9).Value IsNot Nothing Then
dictionary.Add(row.Index, CType(row.Cells(9).Value, Double))
End If
Next
Dim sorted = From keyValuePair In dictionary
Order By keyValuePair.Value Descending
dgvWASPAS.Rows(sorted(0).Key).DefaultCellStyle.BackColor = Color.DarkGreen
dgvWASPAS.Rows(sorted(0).Key).DefaultCellStyle.ForeColor = Color.White
dgvWASPAS.Rows(sorted(1).Key).DefaultCellStyle.BackColor = Color.DarkSeaGreen
dgvWASPAS.Rows(sorted(1).Key).DefaultCellStyle.ForeColor = Color.Black
dgvWASPAS.Rows(sorted(2).Key).DefaultCellStyle.BackColor = Color.LightGreen
dgvWASPAS.Rows(sorted(2).Key).DefaultCellStyle.ForeColor = Color.Black
End Sub
根据 OP 评论进行编辑。
让我看看我是否正确。
代码被赋予了一个“目标”值。在本例中为“0.69”。
如果有三 (3) 个或更多值大于“>”目标值,那么,仅突出显示大于目标值的三个最高值。
和
如果小于三 (3) 个值大于“>”目标值,则仅突出显示这些值。
如果这是正确的,则需要对当前代码进行小幅调整。
由于我们已经将所有值从高到低排序,一个简单的解决方案是遍历前三个值并检查这些值是否大于目标值。这个简单的更改可能如下所示。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dictionary As Dictionary(Of Integer, Double) = New Dictionary(Of Integer, Double)()
For Each row As DataGridViewRow In dgvWASPAS.Rows
If row.Cells(0).Value IsNot Nothing Then
dictionary.Add(row.Index, CType(row.Cells(0).Value, Double))
End If
Next
Dim sorted = From keyValuePair In dictionary
Order By keyValuePair.Value Descending
Dim targetValue = 0.69
For index = 0 To 2
If sorted(index).Value > targetValue Then
dgvWASPAS.Rows(sorted(index).Key).DefaultCellStyle.BackColor = Color.DarkGreen
dgvWASPAS.Rows(sorted(index).Key).DefaultCellStyle.ForeColor = Color.White
End If
Next
End Sub
初学者问题。因此,我在 VB.Net 上有一个项目,其中我 需要突出显示 DataGridView 中的前三行 (按最高值)。我该怎么做?
在这个程序中,我没有使用数据库来存储值,因为所有内容都是生成的,显示在 DataGridView 中,然后突出显示(如我所料)。
规则(伪代码)
If (column="WASPAS")>6.69 then
If(row>3) Then Highlight 3 Rows
ElseIf(row<3) Then Highlight the Rest of Rows
End If
我现在的代码:
For Each row As DataGridViewRow In dgvWASPAS.Rows
If row.Cells(9).Value > 0.69 Then
row.DefaultCellStyle.ForeColor = Color.White
row.DefaultCellStyle.BackColor = Color.Green
End If
Next
我的数据类型是双精度。在这里,我附上我的 DataGridView 看起来像 DataGridView
请帮助我,我被困在这里几天了。感谢您的帮助。
下面创建一个dictionary
,将网格中每一行的行索引和value
添加到dictionary
,然后代码得到一个排序版本的字典排序降序 value
,然后为前三行着色。
下面的代码使用了...
Dim dictionary As Dictionary(Of Integer, Double)
Dictionary
,存储网格行索引和单元格值。该代码将遍历网格中的每一行,并将每一行的索引和值作为一个项目存储在 dictionary.
填充 dictionary
后,代码将创建一个新的 LINQ 可枚举,通过降序排序(排序)项目...
Dim sorted = From keyValuePair In dictionary
Order By keyValuePair.Value Descending
显然,排序后,最大值将是 sorted
集合中的第一项。然后下一个等等……完整的例子可能看起来像……
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dictionary As Dictionary(Of Integer, Double) = New Dictionary(Of Integer, Double)()
For Each row As DataGridViewRow In dgvWASPAS.Rows
If row.Cells(9).Value IsNot Nothing Then
dictionary.Add(row.Index, CType(row.Cells(9).Value, Double))
End If
Next
Dim sorted = From keyValuePair In dictionary
Order By keyValuePair.Value Descending
dgvWASPAS.Rows(sorted(0).Key).DefaultCellStyle.BackColor = Color.DarkGreen
dgvWASPAS.Rows(sorted(0).Key).DefaultCellStyle.ForeColor = Color.White
dgvWASPAS.Rows(sorted(1).Key).DefaultCellStyle.BackColor = Color.DarkSeaGreen
dgvWASPAS.Rows(sorted(1).Key).DefaultCellStyle.ForeColor = Color.Black
dgvWASPAS.Rows(sorted(2).Key).DefaultCellStyle.BackColor = Color.LightGreen
dgvWASPAS.Rows(sorted(2).Key).DefaultCellStyle.ForeColor = Color.Black
End Sub
根据 OP 评论进行编辑。
让我看看我是否正确。
代码被赋予了一个“目标”值。在本例中为“0.69”。
如果有三 (3) 个或更多值大于“>”目标值,那么,仅突出显示大于目标值的三个最高值。
和
如果小于三 (3) 个值大于“>”目标值,则仅突出显示这些值。
如果这是正确的,则需要对当前代码进行小幅调整。
由于我们已经将所有值从高到低排序,一个简单的解决方案是遍历前三个值并检查这些值是否大于目标值。这个简单的更改可能如下所示。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dictionary As Dictionary(Of Integer, Double) = New Dictionary(Of Integer, Double)()
For Each row As DataGridViewRow In dgvWASPAS.Rows
If row.Cells(0).Value IsNot Nothing Then
dictionary.Add(row.Index, CType(row.Cells(0).Value, Double))
End If
Next
Dim sorted = From keyValuePair In dictionary
Order By keyValuePair.Value Descending
Dim targetValue = 0.69
For index = 0 To 2
If sorted(index).Value > targetValue Then
dgvWASPAS.Rows(sorted(index).Key).DefaultCellStyle.BackColor = Color.DarkGreen
dgvWASPAS.Rows(sorted(index).Key).DefaultCellStyle.ForeColor = Color.White
End If
Next
End Sub