2014-10-22 64 views
0

如何計算標題中具有相同前兩個字符的數據表列(動態創建)?這是我的代碼到目前爲止,但不工作。計算特定的數據表列

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 
+0

請參閱下面的答案。另外,爲什麼你從'3'開始,而不是'For'循環中的'0'? – Shiva 2014-10-22 01:14:03

回答

1

這是因爲您將整個列計數(即dt.Columns.Count)分配給計數器,而不是如果找到它們將它們加1。

試試這個。

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 
+1

啊..我明白了:)謝謝。列索引0到2是靜態列,只有列(3)up是動態創建的,這就是爲什麼循環從3開始。 – eirishainjel 2014-10-22 01:16:34