【问题标题】:Select the row in data grid view在数据网格视图中选择行
【发布时间】:2017-11-22 05:36:48
【问题描述】:

我有一个包含 4 列的数据网格视图。 我只是想这样做,当用户在文本框中写入值并单击按钮时,它将突出显示具有该值的行。

我试过这段代码,但没有用

For Each row As DataGridViewRow In DataGridView2.Rows
    If row.Cells(0).Value.ToString = "'" & txtSearch2.Text & "'" Then

        Dim index As Integer = row.Index

        DataGridView2.Rows(row.Index).Selected = True

    End If
Next

这是一个可视化的基本程序。 欢迎任何帮助。 提前致谢

【问题讨论】:

  • 为什么将文本框文本括在单引号内?这些引号是否出现在网格单元格中?
  • 不,这些都不是
  • @Steve 我删除了单引号,但仍然收到此错误 {"Object reference not set to an instance of an object."}

标签: vb.net winforms datagridview


【解决方案1】:

使用以下代码,这会将所有匹配的单元格背景颜色更改为黄色:

DataGridView2.ClearSelection()

For Each row As DataGridViewRow In DataGridView2.Rows
    For each cell As DataGridViewCell in row.Cells

        If cell.Value Is Nothing Then Continue For

        If CStr(cell.Value).Tolower.Contains(txtSearch2.Text.Tolower) Then

            cell.Selected = True

            'Yellow background When matched
            cell.Style.BackColor = Color.Yellow

        End If
    Next

Next

您可以在这个类似的问题中找到更多有用的信息:

【讨论】:

  • 这里有错误StringComparison.OrdinalIgnoreCase
  • 不工作:(仍然得到空引用的错误
  • 尝试添加If cell.Value Is Nothing Then Continue For查看我的答案更新
猜你喜欢
  • 2010-09-09
  • 1970-01-01
  • 2016-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-18
相关资源
最近更新 更多