-4

I'm trying to hide columns of the gridview that are null or empty and when I debug, at the first for loop it throws the object reference not set to instance of an object. I have tried for quite some time but I can't figure it out. Here is my code.

Protected Sub GridView1_RowDataBound(ByVal sender As Object, 
            ByVal e As GridViewRowEventArgs)
    Dim Grid As GridView = FormView1.FindControl("GridView1")

    Dim hasData As Boolean = False
    Dim row As Integer

    For col = 0 To Grid.HeaderRow.Cells.Count Step 1

        For row = 0 To Grid.Rows.Count Step 1

            If Not (String.IsNullOrEmpty(Grid.Rows(row).Cells(col).Text)) Then
                hasData = True
            End If

        Next

        Grid.Columns(col).Visible = hasData
    Next

End Sub
John Saunders
  • 160,644
  • 26
  • 247
  • 397
user3841709
  • 386
  • 6
  • 28
  • on what line does it happen, why make us guess? – Ňɏssa Pøngjǣrdenlarp Jul 18 '14 at 16:43
  • 1
    It's not a good idea to loop until Count is reach. Loop until (Count-1). If you debug the program with it crash, you'll notice that row and col are probably our of range. – the_lotus Jul 18 '14 at 16:44
  • Yea I correct the count - 1, I missed that. – user3841709 Jul 18 '14 at 16:49
  • What is the object that it's trying to reference? I have trying creating a new instance of the gridview and that wasn't the problem – user3841709 Jul 18 '14 at 16:51
  • Why do you want to loop through `Grid.Rows.Count` within `GridView1_RowDataBound`? The `RowDataBound` by itself is executed for each row in the grid so you don't need to loop through again. – Dennis R Jul 18 '14 at 16:54
  • I am just trying to hide all of the columns in the gridview that are empty and while I was trying to look this up online most all of the solutions I saw were by using loops. – user3841709 Jul 18 '14 at 17:01
  • So do you want to hide the gridview column if any of the rows for that corresponding column has an empty or null value? – Dennis R Jul 18 '14 at 17:07
  • Yes. The dataSource for that gridview is pulling Columns from many tables. I am passing a query string ID on page load and need the gridview to pull data from all these tables. However, many of the column names are different so an ID value from Table1 is not going to have the same data returned as Table2. I just need to hide all the null columns because there are around 30 extra columns showing that are all blank. – user3841709 Jul 18 '14 at 17:12
  • I mentioned in my question that the error is thrown on the line of the first for loop – user3841709 Jul 18 '14 at 20:51

2 Answers2

0

your missing a few imporant pieces. here is something for you to look over and see if you get it.

             For Each clm As DataGridViewColumn In grdView.Columns
                Dim notAvailable As Boolean = True

                For Each row As DataGridViewRow In grdView.Rows
                    If Not String.IsNullOrEmpty(row.Cells(clm.Index).Value.ToString()) Then
                        notAvailable = False
                        Exit For
                    End If
                Next
                If notAvailable Then
                    grdView.Columns(clm.Index).Visible = False
                End If
            Next
0

Try something like this. If all the rows in a column are empty, then that column will be hidden. Thus, if there is even one data available in a column, it will not be hidden. This code assumes that you have all bound columns in the gridview.

You can call this method after the DataBound() event for the gridview and it need not go into the RowDataBound() event.

GridView1.DataSource = getGridDate() // your data source
GridView1.DataBind()
HideEmptyColumns ()

and the HideEmptyColumns () method

Private Sub HideEmptyColumns()
    Dim bHasValue As Boolean
      For iCol As Integer = 0 To GridView1.ColumnCount - 1
        bHasValue = False
        For iRow As Integer = 0 To GridView1.RowCount - 1
            If GridView1.Rows(iRow).Cells(iCol).Text != String.Empty Then
                bHasValue = True
                Exit For
            End If
        Next

        'Hide the column
        If bHasValue = False Then
            GridView1.Columns(iCol).Visible = False
        End If
    Next

End Sub
Dennis R
  • 3,195
  • 1
  • 19
  • 24