简体   繁体   English

计算特定的数据表列

[英]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. 这是因为您正在将整个列计数(即dt.Columns.Count )分配给计数器,而不是将计数器增加1。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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