Bobski Bobski - 20 days ago 5 Question

Looping through a specific dataset column to check if it contains a certain value

I load a datagridview and am trying to find a quick way of looping through a certain column, given column name....

This is how my datagridview is loaded....

ad = New SqlDataAdapter(sql, con)
ad.Fill(ds, "Fields")
dgvFields.DataSource = ds.Tables("Fields")

dgvFields.ReadOnly = True
dgvFields.RowHeadersVisible = False

dgvFields.Columns(0).Width = 156
dgvFields.Columns(1).Width = 156
dgvFields.Columns(2).Width = 156
dgvFields.Columns(3).Width = 157

'Now I want to be able to loop through dgvfields.columns(3) or column name "Field1a" and check if dgvfields.rows="ValueA"

something along the lines...

For Each row As DataRow In dgvFields.Columns(5)
if dgvfields.rows.value="ValueA" then
'do something
End if



The issue is the type you are looking at for your row... DataRow is for a DataTable not a DataGridViewRow... I would recommend using the index...

Note: this can be done many way's.

For i As Integer = 0 To dgvFields.Rows.Count - 1
       If dgvfields.Rows(i).Cells("THE COLUMN NAME").Value="ValueA" Then
          'do something
       End if