title: How to check if Cell In Datagridview has a value and continue VB.Net
body: <p>hi my code is trying to </p>
  
  <ul>
  <li><p>First: Find if value is in a Column 0 "CODE"</p></li>
  <li><p>Second: If found check if Column 1 "STATUS" has value of "IN"
             If true MsgBox Appears with Message "Ticket Used"
             If Value is nothing then Change value to IN</p></li>
  </ul>
  
  <p>this is my code:</p>
  
  <pre><code>    Private Sub changefound()
  
      Dim findtxt As String = txt_Find.Text
      Try
          If DataGridView2.Rows.Count &gt; 0 Then
              For i As Integer = 0 To DataGridView2.Rows.Count - 1
                  ' For j As Integer = 0 To DataGridView2.Columns.Count - 1 'Not using this because we only want to look in CODE column.
                  Dim CellChange As String = DataGridView2.Rows(i).Cells("CODE").Value.ToString
  
                  If CellChange.Contains(findtxt) = True Then
                      If DataGridView2.Rows(i).Cells("STATUS").Value = "IN" Then    'This is Line 366
                          MsgBox("Tickets USed")
                          Exit Sub
  
  
                      Else
                          With DataGridView2
                              ' .Rows(i).Cells(j).Style.BackColor = Color.Gray
                              '  .Rows(i).Cells(j).Value = findtxt
                              ' MsgBox(i &amp; j + 1)
  
                              .Rows(i).Cells("STATUS").Value = "IN"
  
  
                              Exit Sub
                          End With
                      End If
                  End If
                      'Next   'This is the Jfor 
              Next
          End If
      Catch e As Exception
          MessageBox.Show(e.ToString())
      End Try
  
  
  End Sub
  </code></pre>
  
  <p>any way i move it around it will check only if IN is found will it MSGBox used ticket.
  If cell has no value it locks up with error: </p>
  
  <blockquote>
    <p>System.InvalidCastException:Operator'=' is not defined for type"DBNull"
    and string "IN"<br>
    At
    main.vb:line366</p>
  </blockquote>
  
  <p>any help?</p>