1

How to count datatable columns (created dynamically) that has the same first two characters in the header? Here's my code so far but is not working.

    For col As Integer = 3 To dt.Columns.Count - 1
        Dim cntLE, cntUE As Integer
        If dt.Columns(col).ColumnName.Substring(0, 2) = "LE" Then
            cntLE = dt.Columns.Count
        ElseIf dt.Columns(col).ColumnName.Substring(0, 2) = "UE" Then
            cntUE = dt.Columns.Count
        End If
    Next
1
  • See my answer below. Also, why are you starting at 3, instead of at 0 in the For loop? Commented Oct 22, 2014 at 1:14

1 Answer 1

2

It's because you are assigning the entire column count (i.e. dt.Columns.Count ) to the counters, instead of increment them by 1 if found.

Try this.

For col As Integer = 3 To dt.Columns.Count - 1
    Dim cntLE, cntUE As Integer
    If dt.Columns(col).ColumnName.Substring(0, 2) = "LE" Then
        cntLE = cntLE + 1
    ElseIf dt.Columns(col).ColumnName.Substring(0, 2) = "UE" Then
        cntUE = cntUE + 1
    End If
Next
Sign up to request clarification or add additional context in comments.

1 Comment

Ah.. I see :) Thank you. Columns index 0 to 2 are static columns, only Column(3) up are created dynamically that's why the loop starts on 3.

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.