如何在不过滤视图中其余数据的情况下突出显示DataGridView中的行?我可以基于文本框中的文本在Windows窗体上过滤DataGridView,它仅显示符合条件的数据。我不想显示所有数据,而是只显示符合搜索条件的行,而不是过滤数据。
感谢任何帮助,我使用的是Visual Basic。
答案 0 :(得分:0)
这主要是从C#到Search for value in DataGridView in a column给出的答案的VB.Net转换。我添加了一些使用“命名”列并清除当前选择的内容。
我将功能合并到独立应用程序的上一个答案中。表单上有一个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
行的内容完全匹配。在此示例中,如果您在文本框中输入456
,它将与第二行匹配。
答案 1 :(得分:0)
'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
我希望这是您要寻找的东西?