Home > Blockchain >  Option Strict On disallows late binding for DataGridView Row get cell item
Option Strict On disallows late binding for DataGridView Row get cell item

Time:12-19

I want to get the value of a column in a DataGridView so that I can check whether it already exists or not.

I have a code here that I want to not get an error for the Option Strict On.

For Each row In materialsGridView.Rows
    If materialCB.Text = row.Cells("Material").Value Then
        flag = True
        Exit For
    End If
Next

row.Cells("Material").Value says Option Strict On disallows late binding.

CodePudding user response:

  1. You need to cast the Rows property to DataGridViewRow
  2. Text returns a String and Value returns Object. So you have different types that you compare with =. That does not work with Option Strict. So one way to fix is to use:

For Each row As DataGridViewRow In materialsGridView.Rows
    If materialCB.Text = row.Cells("Material").Value.ToString() Then
        flag = True
        Exit For
    End If
Next

Another way would be to use Equals:

For Each row As DataGridViewRow In materialsGridView.Rows
    If materialCB.Text.Equals(row.Cells("Material").Value) Then
        flag = True
        Exit For
    End If
Next
  • Related