我知道这个问题的变体已经被问了很多,我一直在寻找答案很长时间,并尝试了许多不同的代码块。这是我现在从 0 循环到 gridview1.rows.count-1 的内容。此代码位于 RowDataBound 事件中。
Dim test As String
For i As Integer = 0 To GridView1.Rows.Count - 1
test = GridView1.Rows(i).Cells.Item(e.Row.RowIndex).Text.ToString
If test = " " Then
e.Row.Cells(i).Visible = False
End If
Next
每次我收到错误:指定的参数超出了有效值的范围。参数名称:本行索引
test = GridView1.Rows(i).Cells.Item(e.Row.RowIndex).Text.ToString
但是,我认为该错误只是因为我超出了行范围,但无法弄清楚如何解决它。如何成功检查所有行和列的每个单元格并隐藏不返回任何内容的列。
我现在可以隐藏每个空白列中的所有实际单元格
Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
Dim GridView1 As GridView = FormView1.FindControl("GridView1")
For Each row As GridViewRow In GridView1.Rows
For i As Integer = 0 To row.Cells.Count - 1
Dim strtest As String = row.Cells(i).Text.ToString
If strtest = " " Then
row.Cells(i).Visible = False
End If
Next
Next
但它仍然不会让我隐藏列,因为使用 autogeneratecolumns 它无法识别任何列,所以代码 GridView1.Columns(i).visible = false 抛出超出范围错误,因为没有范围列