从 datagridview 检索文本时,对象引用未设置到对象的实例

Object reference not set to an instance of an object on retrieving text from datagridview

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

Object reference not set to an instance of an object.

它指向行:

Dim cell As 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

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

喜欢就去做;

 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

希望对您有所帮助...!!!