Search specific named column in DataGridView

GoodJuJu

Member
Joined
Apr 26, 2010
Messages
24
Location
England
Programming Experience
5-10
Hi there,

I wonder if anybody can help me.... I am looping through a DataGridView and checking the returned string value (strTag) against another string I have (strMyString). My problem is that I have several columns, but I only want to iterate through the Column Named 'TAG_NUMBER'.

Here is what I have so far.. it is working, apart from the fact it appears to be looping through all the columns.
VB.NET:
        Dim strTag as String

        Dim dv As New DataView(dbMyDbase.Tables(0))

            dv.Sort = "TAG_NUMBER"

            For rc As Integer = 0 To dv.Table.Rows.Count - 1
                For cc As Integer = 0 To dv.Table.Columns.Count - 1
                
                    If dv.Table.Columns.Contains("TAG_NUMBER") Then
                    
                        strTag = (dv.Table.Rows(rc)(cc).ToString)

                        If strMyString.Contains(LCase(strTag)) = True Then
                            msgBox("Found " & strTag)
                        End If
                        
                    End If
                    
                Next
            Next

Any help anybody can offer is much appreciated!!!
 
Last edited:
Changed the if statement with .Contains, this method returns a boolean value so there is no need to compare it to True/False just use it as is.

Also when you post code try to use the code tags ( when typing your post it's the # in the editor).
VB.NET:
Dim strTag as String

Dim dv As New DataView(dbMyDbase.Tables(0))

dv.Sort = "TAG_NUMBER"

For rc As Integer = 0 To dv.Table.Rows.Count - 1
     
     'set to lower case
     strTag = dv.Table.Rows(rc)("TAG_NUMBER").ToString().ToLower()
     
     'do the contains on both strings in lowercase
     If strMyString.ToLower().Contains(strTag) Then
          MessageBox.Show("Found " & strTag)
     End If
    
Next
 
demausdauth,

Thanks - your answer was exactly what I was looking for, thanks for the tip about formatting the coding too.
 
Last edited:
Back
Top