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

时间:2015-06-12 05:03:15

标签: datagridview vb.net-2010

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

  

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

它指向这一行:

  

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

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

1 个答案:

答案 0 :(得分:2)

喜欢它;

 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

希望它有所帮助...... !!!