2011-07-14 10 views
1

このコードを読んでください:のGridView:スキップ特定の列

Dim dt As New DataTable 

    For Each col As DataControlField In GridView1.Columns 
     dt.Columns.Add(col.HeaderText) 
    Next 

    For Each row As GridViewRow In GridView1.Rows 
     Dim nrow As DataRow = dt.NewRow 
     Dim z As Integer = 0 
     For Each col As DataControlField In GridView1.Columns 
      nrow(z) = row.Cells(z).Text.Replace(" ", "") 
      z += 1 
     Next 
     dt.Rows.Add(nrow) 
    Next 

どのように私は、GridViewの特定の列をスキップすることができますか?例:列1,3、および6

ありがとうございました!

答えて

0
Dim dt As New DataTable 
For Each col As DataControlField In GridView1.Columns 
If (col.Index <> 1) and (col.Index <> 3) and (col.Index <> 6) then 
dt.Columns.Add(col.HeaderText) 
End If 
Next 
For Each row As GridViewRow In GridView1.Rows 
Dim nrow As DataRow = dt.NewRow 
Dim z As Integer = 0 
For Each col As DataControlField In GridView1.Columns 
If (col.Index <> 1) and (col.Index <> 3) and (col.Index <> 6) then 
nrow(z) = row.Cells(z).Text.Replace("&nbsp;", "") 
z += 1 
End If 
Next 
dt.Rows.Add(nrow) 
Next 

あなたが望むものに近いとは限りませんか?

For-Eachイテレータのループはすべての要素を通過し、不要な要素を図示のようにフィルタリングできます。

または、処理したいすべてのインデックスの配列を作成し、その配列を反復処理して、必要なインデックスのみで列を処理することができます。