【问题标题】:Object reference not set to an instance of an object on retrieving text from datagridview从 datagridview 检索文本时未将对象引用设置为对象的实例
【发布时间】:2015-06-12 05:03:15
【问题描述】:

我有一个使用绑定源绑定到数据库的 Datagridview。我想突出显示具有我正在搜索的值的行或文本。但由于某种原因,它给了我这个错误:

对象引用未设置为对象的实例。

它指向线:

将单元格调暗为 DataGridViewCell = (EmployeesDataGridView.Rows(gridRow).Cells(gridColumn))

Dim someText As String = txtSearchLastName.Text
    Dim gridRow As Integer = 0
    Dim gridColumn As Integer = 0
    For Each Row As DataGridViewRow In EmployeesDataGridView.Rows
        For Each column As DataGridViewColumn In EmployeesDataGridView.Columns
            Dim cell As DataGridViewCell = (EmployeesDataGridView.Rows(gridRow).Cells(gridColumn))
            If cell.Value.ToString.ToUpper.Contains(someText.ToUpper) Then
                cell.Style.BackColor = Color.Yellow
            End If
            gridColumn += 1
        Next column
        gridColumn = 0
        gridRow += 1
    Next Row

我已阅读此错误的含义,但无法将其含义与我的代码相关联。谢谢。

【问题讨论】:

  • 您需要搜索所有单元格吗?还是固定列中的单元格?

标签: datagridview vb.net-2010


【解决方案1】:

照做;

 For Each row As DataGridViewRow In EmployeesDataGridView.rows

     For Each cell As DataGridViewCell In row.cells

          If cell.Value.ToString.ToUpper.Contains(someText.ToUpper) Then
               cell.Style.BackColor = Color.Yellow
          End If

     Next

 Next

希望对你有帮助...!!!

【讨论】:

  • 我找到了错误的原因。这是当它到达没有记录的行时。我可以通过取消选中 datagridview 中的启用添加或者如果我使用 datagridview.rows.count - 1 来更正它。但是我使用了你的 sn-p 并且它有效。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-23
  • 2017-02-01
  • 1970-01-01
  • 2015-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多