请阅读此代码:
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 :(得分: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(" ", "")
z += 1
End If
Next
dt.Rows.Add(nrow)
Next
那不就是你想要的吗?
For-Each迭代器循环遍历每个元素,您可以过滤掉不需要的元素,如图所示。
或者,您可以创建一个包含要处理的所有索引的数组,并迭代该数组以仅处理仅包含所需索引的列。