简体   繁体   中英

Count specific datatable columns

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

It's because you are assigning the entire column count (ie 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM