【问题标题】:Highlight row in a DataGridView without filtering the rest of the data突出显示 DataGridView 中的行而不过滤其余数据
【发布时间】:2018-06-21 10:46:59
【问题描述】:

如何在 DataGridView 中突出显示一行而不过滤视图中的其余数据?我可以根据文本框中的文本过滤 Windows 窗体上的 DataGridView,它只向我显示符合条件的数据。我不想过滤数据,而是想显示所有数据,但只突出显示符合搜索条件的行。

感谢任何帮助,我正在使用 Visual Basic。

【问题讨论】:

  • 到目前为止,您尝试或尝试过什么?类似...也许,设置DataGridViewSelectedRow
  • 如何使用基于搜索的选定行?
  • 这是一个 C# 版本。 Search for value in DataGridView in a column。如果您无法翻译到 VB.net,请告诉我
  • 谢谢。你能把它转换成VB吗?
  • 我转换了代码,但它仍然只显示与搜索匹配的行。我希望它突出显示它,而不是过滤结果。

标签: vb.net


【解决方案1】:

这主要是从 C# 到 VB.Net 的翻译,答案是:Search for value in DataGridView in a column。我已经添加了使用命名列并清除当前选择的内容。

我在上一个独立应用程序的答案中加入了该功能。 Form 上有一个 DataGridView、Button 和一个 TextBox,其功能如下。

Public Class Form1
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        InitialiseData()
        DataGridView1.DataSource = People
    End Sub

    Private People As List(Of Person)

    Private Sub InitialiseData()
        People = New List(Of Person)()
        People.Add(New Person With {.Name = "J1", .Address = "Address1", .Phone = "123"})
        People.Add(New Person With {.Name = "J2", .Address = "Address2", .Phone = "456"})
        People.Add(New Person With {.Name = "J3", .Address = "Address3", .Phone = "789"})
        People.Add(New Person With {.Name = "J4", .Address = "Address4", .Phone = "147"})
        People.Add(New Person With {.Name = "J5", .Address = "Address5", .Phone = "258"})
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        Dim searchValue As String = TextBox1.Text

        Dim searchColumn As DataGridViewColumn = DataGridView1.Columns("Phone")

        DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DataGridView1.ClearSelection()
        Try
            For Each row As DataGridViewRow In DataGridView1.Rows

                If (row.Cells(searchColumn.Index).Value.ToString().Equals(searchValue)) Then
                    row.Selected = True
                    Exit For
                End If
            Next
        Catch exc As Exception
            MessageBox.Show(exc.Message)
        End Try
    End Sub
End Class

Public Class Person
    Public Property Name As String
    Public Property Address As String
    Public Property Phone As String
End Class

Dim searchColumn As DataGridViewColumn = DataGridView1.Columns("Phone") 行将Phone 列设置为搜索条件的目标。

If (row.Cells(searchColumn.Index).Value.ToString().Equals(searchValue)) Then 行内容完全匹配。在本例中,如果您在 TextBox 中输入 456,它将与第二行匹配。

【讨论】:

    【解决方案2】:
    'search amount of rows'   
     For i = 0 To yourDatagridview.Rows.Count - 1 
        'if it contains what you have typed in "yourtextbox"
            If (yourDatagridview.Rows(i).Cells("date").Value) = yourTextBox.Text Then
        'make it red'
                yourDatagridview.Rows(i).DefaultCellStyle.BackColor = Color.Red
    
            End If
    
        Next
    

    我希望这就是你要找的东西?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-17
      • 2019-11-05
      • 1970-01-01
      • 2017-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-18
      相关资源
      最近更新 更多