VB row.Cells("XX").Selected = True 没有真正选中
VB row.Cells("XX").Selected = True is not really selected
我想在搜索期间 select 行:
代码:
Private Sub TextBoxSEARCH_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBoxSEARCH.TextChanged
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells("Column1").Value = TextBoxSEARCH.Text Then
DataGridView1.ClearSelection()
row.Cells("Column1").Selected = True
Exit For
End If
Next
End Sub
一切正常,它也 !show!当它找到但它不是真正的 selected 时,我在右行,它与您单击它的方式不同。例如,我有一个按钮正在更改 selected 行的背景颜色,在搜索后,它为第一行着色,即开始时 selected。
怎么改?
谢谢。
我刚刚测试过,它确实按照您描述的方式工作。我使用了这段代码:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim table As New DataTable
With table.Columns
.Add("Id", GetType(Integer))
.Add("Name", GetType(String))
.Add("DoB", GetType(Date))
End With
With table.Rows
.Add(1, "Peter", #6/19/1969#)
.Add(2, "Paul", #1/1/2000#)
.Add(3, "Mary", #5/3/2021#)
End With
BindingSource1.DataSource = table
DataGridView1.DataSource = BindingSource1
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
DataGridView1.ClearSelection()
Dim matchingRow = DataGridView1.Rows.
Cast(Of DataGridViewRow).
FirstOrDefault(Function(dgvr) CStr(dgvr.Cells(1).Value) = TextBox1.Text)
If matchingRow IsNot Nothing Then
matchingRow.Selected = True
End If
End Sub
并且它完全按照预期工作。如果你没有看到,那么要么你的系统坏了,要么你实际上没有匹配文本。请记住,您正在进行区分大小写的相等比较,因此您必须匹配大小写以及文本中的字母。如果这不是你想要的,那么你需要一个不区分大小写的比较:
FirstOrDefault(Function(dgvr) CStr(dgvr.Cells(1).Value).Equals(TextBox1.Text, StringComparison.CurrentCultureIgnoreCase))
编辑:
首先,如果 SelectionMode
设置为 FullRowSelect
那么你可以 select 一行:
matchingRow.Selected = True
或一个单元格:
matchingRow.Cells(1).Selected = True
其次,如果网格包含数据输入行,我展示的不区分大小写的比较将抛出 NullReferenceException
,因为单元格的 Value
将是 Nothing
等 Equals
不能调用它。要解决这个问题,比较必须更复杂一些:
Dim matchingRow = DataGridView1.Rows.
Cast(Of DataGridViewRow).
FirstOrDefault(Function(dgvr)
Dim result = CStr(dgvr.Cells(1).Value)?.Equals(TextBox1.Text, StringComparison.CurrentCultureIgnoreCase)
Return result.HasValue AndAlso result.Value = True
End Function)
最后,问题实际上与 selection 无关,实际上是如何使匹配行成为当前行,这要求该行中的一个单元格成为当前行细胞。这取决于您要将插入符放在哪个单元格中。我认为是第一个单元格还是进行比较的单元格,但可能有理由使用不同的单元格:
If matchingRow IsNot Nothing Then
matchingRow.Cells(1).Selected = True
DataGridView1.CurrentCell = matchingRow.Cells(1)
End If
我想在搜索期间 select 行: 代码:
Private Sub TextBoxSEARCH_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBoxSEARCH.TextChanged
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells("Column1").Value = TextBoxSEARCH.Text Then
DataGridView1.ClearSelection()
row.Cells("Column1").Selected = True
Exit For
End If
Next
End Sub
一切正常,它也 !show!当它找到但它不是真正的 selected 时,我在右行,它与您单击它的方式不同。例如,我有一个按钮正在更改 selected 行的背景颜色,在搜索后,它为第一行着色,即开始时 selected。
怎么改? 谢谢。
我刚刚测试过,它确实按照您描述的方式工作。我使用了这段代码:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim table As New DataTable
With table.Columns
.Add("Id", GetType(Integer))
.Add("Name", GetType(String))
.Add("DoB", GetType(Date))
End With
With table.Rows
.Add(1, "Peter", #6/19/1969#)
.Add(2, "Paul", #1/1/2000#)
.Add(3, "Mary", #5/3/2021#)
End With
BindingSource1.DataSource = table
DataGridView1.DataSource = BindingSource1
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
DataGridView1.ClearSelection()
Dim matchingRow = DataGridView1.Rows.
Cast(Of DataGridViewRow).
FirstOrDefault(Function(dgvr) CStr(dgvr.Cells(1).Value) = TextBox1.Text)
If matchingRow IsNot Nothing Then
matchingRow.Selected = True
End If
End Sub
并且它完全按照预期工作。如果你没有看到,那么要么你的系统坏了,要么你实际上没有匹配文本。请记住,您正在进行区分大小写的相等比较,因此您必须匹配大小写以及文本中的字母。如果这不是你想要的,那么你需要一个不区分大小写的比较:
FirstOrDefault(Function(dgvr) CStr(dgvr.Cells(1).Value).Equals(TextBox1.Text, StringComparison.CurrentCultureIgnoreCase))
编辑:
首先,如果 SelectionMode
设置为 FullRowSelect
那么你可以 select 一行:
matchingRow.Selected = True
或一个单元格:
matchingRow.Cells(1).Selected = True
其次,如果网格包含数据输入行,我展示的不区分大小写的比较将抛出 NullReferenceException
,因为单元格的 Value
将是 Nothing
等 Equals
不能调用它。要解决这个问题,比较必须更复杂一些:
Dim matchingRow = DataGridView1.Rows.
Cast(Of DataGridViewRow).
FirstOrDefault(Function(dgvr)
Dim result = CStr(dgvr.Cells(1).Value)?.Equals(TextBox1.Text, StringComparison.CurrentCultureIgnoreCase)
Return result.HasValue AndAlso result.Value = True
End Function)
最后,问题实际上与 selection 无关,实际上是如何使匹配行成为当前行,这要求该行中的一个单元格成为当前行细胞。这取决于您要将插入符放在哪个单元格中。我认为是第一个单元格还是进行比较的单元格,但可能有理由使用不同的单元格:
If matchingRow IsNot Nothing Then
matchingRow.Cells(1).Selected = True
DataGridView1.CurrentCell = matchingRow.Cells(1)
End If