Vb.net 高亮显示DataGridView中的行,而不过滤其余数据
如何突出显示DataGridView中的一行,而不过滤视图中的其余数据?我可以根据文本框中的文本过滤windows窗体上的DataGridView,它只显示符合条件的数据。我不想过滤数据,而是想显示所有数据,只是突出显示符合搜索条件的行Vb.net 高亮显示DataGridView中的行,而不过滤其余数据,vb.net,Vb.net,如何突出显示DataGridView中的一行,而不过滤视图中的其余数据?我可以根据文本框中的文本过滤windows窗体上的DataGridView,它只显示符合条件的数据。我不想过滤数据,而是想显示所有数据,只是突出显示符合搜索条件的行 感谢您的帮助,我正在使用visual basic。这主要是从C#到VB.Net的翻译,答案如下:。我添加了使用命名列和清除当前选择的内容 我将该函数合并到一个独立应用程序的上一个答案中。表单上有一个DataGridView、按钮和一个文本框,其函数如下所示 Pu
感谢您的帮助,我正在使用visual basic。这主要是从C#到VB.Net的翻译,答案如下:。我添加了使用命名列和清除当前选择的内容 我将该函数合并到一个独立应用程序的上一个答案中。表单上有一个DataGridView、按钮和一个文本框,其函数如下所示
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
列设置为搜索条件的目标
行如果(row.Cells(searchColumn.Index).Value.ToString().Equals(searchValue)),则执行精确的内容匹配。在本例中,如果在文本框中输入456
,它将与第二行匹配
'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
我希望这就是你想要的?到目前为止,你尝试或试图做什么?类似于……也许,设置DataGridView
的SelectedRow
?如何基于搜索使用所选行?这是一个C#verison。如果您无法翻译到VB.net,请告诉我。谢谢。你能把它转换成VB吗?我转换了代码,但它仍然只显示与搜索匹配的行。我希望它突出显示,而不是过滤结果。