Question

I have a gridview something like this. It is binded in a datasource. Now, what I need is to hide the columns that its rows has no data available. In the table example below, the column with "2ND" as HeaderText should be hidden since it has no data.

enter image description here

Protected Sub grdsf_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdsf.DataBound
        Dim rowscount As Integer = grdsf.Rows.Count
        Dim columnscount As Integer = grdsf.Columns.Count
        Dim k As Integer = 0
        For j As Integer = 1 To columnscount - 1 Step j + 1
            For i As Integer = 0 To rowscount - 1 step i + 1
                Dim x As String = grdsf.Rows(i).Cells(j).Text
                If x = String.Empty Then
                    k = k + 1
                End If
            Next
            Dim col As DataControlField = grdsf.Columns(j)
            If k = rowscount Then
                col.Visible = False
            End If

        Next
    End Sub

The loop for column starts at index 1 since the 1st column should not be hidden..

<asp:GridView ID="grdsf" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1" GridLines="Horizontal" ShowFooter="True" Width="90%">
<RowStyle ForeColor="#000066" />
<FooterStyle BackColor="White" ForeColor="#000066" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
<asp:BoundField DataField="LNAME" HeaderText="LNAME" SortExpression="LNAME" />
<asp:BoundField DataField="OYRGR" HeaderText="OYRGR" SortExpression="OYRGR" />
<asp:BoundField DataField="Swimming" HeaderText="Swimming" ReadOnly="True" SortExpression="Swimming" NullDisplayText="" />
<asp:BoundField DataField="Soccer" HeaderText="Soccer" ReadOnly="True" SortExpression="Soccer" NullDisplayText="" />
<asp:BoundField DataField="Softball" HeaderText="Softball" ReadOnly="True" SortExpression="Softball" NullDisplayText="" />
<asp:BoundField DataField="Baseball" HeaderText="Baseball" ReadOnly="True" SortExpression="Baseball" NullDisplayText="" />
<asp:BoundField DataField="Volleyball" HeaderText="Volleyball" ReadOnly="True" SortExpression="Volleyball" />
<asp:BoundField DataField="Basketball" HeaderText="Basketball" ReadOnly="True" SortExpression="Basketball" />
<asp:BoundField DataField="Martial Arts" HeaderText="Martial Arts" ReadOnly="True" SortExpression="Martial Arts" />
<asp:BoundField DataField="Karate" HeaderText="Karate" ReadOnly="True" SortExpression="Karate" />
<asp:BoundField DataField="Arnis" HeaderText="Arnis" ReadOnly="True" SortExpression="Arnis" />
<asp:BoundField DataField="Taekwondo" HeaderText="Taekwondo" ReadOnly="True" SortExpression="Taekwondo" />
<asp:BoundField DataField="Judo" HeaderText="Judo" ReadOnly="True" SortExpression="Judo" />
<asp:BoundField DataField="Boxing" HeaderText="Boxing" ReadOnly="True" SortExpression="Boxing" />
<asp:BoundField DataField="Wrestling" HeaderText="Wrestling" ReadOnly="True" SortExpression="Wrestling" />
<asp:BoundField DataField="Unit Physical Fitness" HeaderText="Unit Physical Fitness" ReadOnly="True" SortExpression="Unit Physical Fitness" />
<asp:BoundField DataField="Gymnastics" HeaderText="Gymnastics" ReadOnly="True" SortExpression="Gymnastics" />
<asp:BoundField DataField="Health and Fitness" HeaderText="Health and Fitness" ReadOnly="True" SortExpression="Health and Fitness" />
<asp:BoundField DataField="Sports Mngt." HeaderText="Sports Mngt." ReadOnly="True" SortExpression="Sports Mngt." />
<asp:BoundField DataField="Badminton" HeaderText="Badminton" ReadOnly="True" SortExpression="Badminton" />
</Columns>
</asp:GridView>
Was it helpful?

Solution

Try this method.

Private Sub HideBlankColumns()
    Dim bHasValue As Boolean
    Dim sValue As String
    For iCol As Integer = 0 To GridView1.ColumnCount - 1
        bHasValue = False
        For iRow As Integer = 0 To GridView1.RowCount - 1
            sValue = (GridView1.Rows(iRow).FindControl("LableID") As Label).Text
            If GridView1.Rows(iRow).Cells(iCol).Text != String.Empty && sValue != String.Empty Then
                bHasValue = True
                Exit For
            End If
        Next
        'To Hide the column
        If bHasValue = False Then
            GridView1.Columns(iCol).Visible = False
        End If

        ' ''To Remove the Column
        ''If bHasValue = False Then
        ''    GridView1.Columns.RemoveAt(iCol)
        ''    iCol -= 1
        ''End If
    Next

End Sub

OTHER TIPS

try this:

For each _Row as GridViewRow in Gridview1.rows
    For each _Col as GridViewColumn in Gridview1.columns

    If Gridview1.rows(_row.index).item(_col.index).value = Nothing Then

          GRidView1.Columns.remove(_col.index) 

    End If

Next
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top