0

我知道这个问题的变体已经被问了很多,我一直在寻找答案很长时间,并尝试了许多不同的代码块。这是我现在从 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 抛出超出范围错误,因为没有范围列

4

3 回答 3

0
For Each row As GridViewRow in GridView1.rows // rows
dim someval as String
 for x= 0 to  GridView1.Columns.Count // cols 
  if someval=  = row.Cells[x].Text;
 GridView1.Columns(x).Visible = false;
end if
 next x
Next

手动输入请检查错别字'

于 2014-07-23T17:16:15.427 回答
0

尝试这样的事情。

但我可能会指出,每次绑定一行时,您似乎都在阅读整个网格,这似乎没有必要。相反,您可以有一个单独的方法,您可以在 gridviewdatabound隐藏列之后调用该方法。

将下面的代码放在一个函数中,并在网格的数据绑定之后调用这个函数。

Private Sub CheckForEmptyValues()
    Dim dgv As DataGridView = GridView1

    For r As Integer = 0 To dgv.RowCount - 1
       For c As Integer = 0 To dgv.ColumnCount - 1
            Dim cellValue as string = dgv.Rows(r).Cells(c).Value
            If cellValue = " " Then
               dgv.Rows(r).Cells(c).Visible = False
            End If
        Next
    Next
End Sub
于 2014-07-23T17:25:51.003 回答
0

Ok Guys I finally found the correct way to do this, at least a way that works for me. So you have to use the row.cells.count -1 to actually get the count of your columns when you have autogeneratecolumns set to true and in order to iterate through your columns to set them to visible = false, you must use

GridView1.HeaderRow.Cells(i).visible = False

Here is my Final code and hopefully this will help a lot of people out that have the same problem, Thank you all for your help and answers

 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
                GridView1.HeaderRow.Cells(i).Visible = False
                row.Cells(i).Visible = False

            End If

        Next
    Next
 End Sub
于 2014-07-24T13:26:45.653 回答