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:
- You need to cast the
Rows
property toDataGridViewRow
Text
returns aString
andValue
returnsObject
. So you have different types that you compare with=
. That does not work withOption 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