0

I have a grid view that has a column containing strings (Middle column).

Gridview

On the rowDataBound event I want to loop through the column looking for the integer it contains and then display a value in the first column.

I know that the integer range will be 1 to 63 so I can use a FOR loop to loop through the numbers. Here is what I have so far.

For x As Integer = 1 To 63

If CType(e.Row.Cells(2).FindControl("lblTagName"), Label).Text Then

End If

Next

The problem I am having is using contains. I cant use the following as it would also be true for the number 1, 10, 11 etc when x = 1.

For x As Integer = 1 To 63

If CType(e.Row.Cells(2).FindControl("lblTagName"), Label).Text.Contains(x) Then

End If

Next

How do I make sure it only gets one result per number? i.e x = 6 would return UMIS.75OPTR6GROSSMARGIN.F_CV and not all the other strings that contain the number 6.

UPDATE - based on some answers I may not of explained this very well. I want to loop through the gridview and if the number 1 is found and only the number 1 in the second column, not 10 etc then I want to display "Run 1" in the first column. So when x = 10 it will show "Run 10" and so on.

UPDATE 2 - its definatley my explanation, apologies.

The resultant grid view would look like this.

Resultant gridview

The order of the second column is not set and is not in order.

1
  • See my updated answer. Commented Feb 21, 2017 at 15:20

3 Answers 3

1

You'd have to check the entire text of the label to determine whether it is only 1, and not 10, 11, 12, 13, ... as well.

Also, in this case you should use DirectCast rather than CType. CType is only used when converting to different types that include conversion operators, here you are always dealing with a label.

For x As Integer = 1 To 63

    If String.Equals(DirectCast(e.Row.Cells(2).FindControl("lblTagName"), Label).Text, "UMIS.75OPTR" & x & "GROSSMARGIN.F_CV", StringComparison.OrdinalIgnoreCase) Then
        'Do your stuff.
    End If

Next
Sign up to request clarification or add additional context in comments.

1 Comment

@Silentbob : No problem, sometimes it's hard to explain to others what you want to do with your code :). Glad I could help! -- Also, note that because I am using StringComparison.OrdinalIgnoreCase the match is case-insensitive. Thus, it won't matter if the label's text is for example umis.75opTr6grOSsmargIN.F_Cv, as long as it's the same characters.
1

You might want to think if doing it the other way around. Get the list of numbers in your string with a regular expression match.

    Dim s As String = "asd12asdasd.sdf3sdf"

    For Each m As System.Text.RegularExpressions.Match In System.Text.RegularExpressions.Regex.Matches(s, "[\d]*")
        If m.Success AndAlso Not String.IsNullOrEmpty(m.Value) Then
            ' m.Value
        End If
    Next

With that list of number, you can check if it's between 1 and 63.

If your string have the same suffix/prefix, just remove them to show you what the number is.

    Dim s As String = "UMIS.75OPTR12GROSSMARGIN.F_CV"
    Dim number As String = s.Replace("UMIS.75OPTR", "").Replace("GROSSMARGIN.F_CV", "")

1 Comment

For the Regex example, keep in mind that his string starts with "UMIS. 75 ".
0

Go backwards in a Do Until Loop:

Dim bolFoundMatch As Boolean = False
Dim intCursor As Integer = 63

Do Until (bolFoundMatch OrElse intCursor = 0)

     If CType(e.Row.Cells(2).FindControl("lblTagName"), Label).Text.Contains(intCursor) Then

          'Anything you want to do when you find your match.

          'This will ensure your loop exits.
          bolFoundMatch = True

     End If 

     intCursor -= 1

Loop

10 Comments

surely if intcursor = 1 then it will stop at 61 whereas i want to go to 1
That's why I have it going backwards. 61 will stop the loop before it gets to 1.
But he wants it to go from 1 and up, not the other way around.
Oh, totally missed that.
and it needs to go through the entire grid. See my update
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.