title: Asp.Net GridView Retrieving cell values of each row through for loop and hiding empty columns
body: <p>I know variations of this question have been asked a lot and I have been looking for answers for a long time and have tried many different blocks of code. Here is what I have right now which I am looping through from 0 to gridview1.rows.count-1. This code is in the RowDataBound Event. </p>
  
  <pre><code>    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 = "&amp;nbsp;" Then
              e.Row.Cells(i).Visible = False
          End If
  
      Next
  </code></pre>
  
  <p>Every time I get the error: Specified argument was out of the range of valid values.
  Parameter name: index on this line</p>
  
  <pre><code>   test = GridView1.Rows(i).Cells.Item(e.Row.RowIndex).Text.ToString
  </code></pre>
  
  <p>However, I think the error is just because I am going outside of the row range but can't figure out how to fix it. How can I successfully check each cell of all the rows and columns and hide the columns that return nothing. </p>
  
  <p>I can now hide all of the actual cells within each column that is blank with this</p>
  
  <pre><code> 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 = "&amp;nbsp;" Then
           row.Cells(i).Visible = False
           End If      
          Next
      Next
  </code></pre>
  
  <p>But it still won't let me hide the columns because with autogeneratecolumns it doesn't recognize that any columns are there so the code, 
      GridView1.Columns(i).visible = false
  Throws the out of range error because there is no range of columns</p>