hide gridview column if there is no data in it

kahvedzic
kahvedzic used Ask the Experts™
on
have a gridview with only one row in any case, what I want to do is to set columns invisble (hide it) if there is no text (no value) in them. Each cell can only have value X or NULL, I want to make every column that have NULL value invisible.

Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
This is just an example i have done before:

The Columns collection only stores the explicitly declared columns, so if you're using autogenerated columns, the count will be zero.  If you're using autogenerated column, after databind you could loop through the rows collection and make the appropriate cells invisible, like:

GridView1.DataBind();
if (GridView1.Columns.Count > 0)
      GridView1.Columns[0].Visible = false;
else
{
      GridView1.HeaderRow.Cells[0].Visible = false;
      foreach (GridViewRow gvr in GridView1.Rows)
      {
            gvr.Cells[0].Visible = false;
      }
}

Hope that helps.

Commented:
Try this:

If e.Row.RowType = DataControlRowType.DataRow Then  
        Dim i As Int16, strCell As String  
        For i = 0 To e.Row.Cells.Count - 1  
             strCell = Trim(e.Row.Cells(i).Text)  
             If strCell = " " Then strCell = "" 
             If String.IsNullOrEmpty(strCell) Then  
                GridView3.Columns(i).Visible = False  
            End If  
        Next i  
    End If

Commented:
If you work with my code then make sure you put that in the Row_Databound event on the Gridview.  Good luck.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Hi kahvedzic,

In the rowdatabound event just add the following code if u have bind the gridview using BoundField

If e.Row.RowType = DataControlRowType.DataRow Then  
        For i = 0 To e.Row.Cells.Count - 1  
             If String.IsNullOrEmpty(e.Row.Cells(i).Text) Then  
                e.Row.Cells(i).Visible = False  
            End If  
        Next i  
    End If

If u have bind the gridview using template field u will have to find the control of each cell and then make it invisible

If e.Row.RowType = DataControlRowType.DataRow Then  
        For i = 0 To e.Row.Cells.Count - 1  
             If String.IsNullOrEmpty(((Label)e.Row.FindControl("Label1")).Text) Then  
                e.Row.Cells(i).Visible = False  
            End If  
        Next i  
    End If

Author

Commented:
@arhame

now it is not loading empty columns. Problem is when I have row that should load all columns (or more than one) it loads, when I after that have row with only one column (or different columns) it loads again, but when I click back on first mentioned row (with all columns) it would load only columns from second clicked row. I have auto generate columns set to false and two gredviews (one with select button and other should have invisible columns).

Commented:
I'm having a hard time understanding your quetion, but I'm going to take a stab in the dark.  So you're saying it makes the columns invisible the first time - but if you make another selection then the columns in that gridview are still invisible from the first time it loaded?  Try this:

If e.Row.RowType = DataControlRowType.DataRow Then  
        Dim i As Int16, strCell As String  
        For i = 0 To e.Row.Cells.Count - 1  
             strCell = Trim(e.Row.Cells(i).Text)  
             If strCell = " " Then strCell = "" 
             If String.IsNullOrEmpty(strCell) Then  
                GridView3.Columns(i).Visible = False  
              Else
                Gridview3.Columns(i).Visible = True
            End If  
        Next i  
    End If

Author

Commented:
I already try this. Maybe simplified,after I click on select in GridView1 Gridview2 loads only columns with data,if I select other row and it has data in other columns than first selected row it wont be loaded. I think that after selecting row I have to refresh gridviews.

Author

Commented:
I have two gridviews  first is with the select button what I need is to load second gridview on select button and in second gridview to display only columns with data and to hide empty columns (this is possible because I will always have one row in second grid). But the problem is when I click on first row I get correct results in second grid, but when I click on second row I have results only in same columns as first row and not other columns, for example for first row I have data only in first column of second grid, and for second row I have data in first,second and third column but only first column will be loaded.

Author

Commented:
I tried this and still have the same problem


If e.Row.RowType = DataControlRowType.DataRow Then  
        Dim i As Int16, strCell As String  
        For i = 0 To e.Row.Cells.Count - 1  
             strCell = Trim(e.Row.Cells(i).Text)  
             If strCell = " " Then strCell = "" 
             If String.IsNullOrEmpty(strCell) Then  
                GridView3.Columns(i).Visible = False  
              Else
                Gridview3.Columns(i).Visible = True
            End If  
        Next i  
    End If

Author

Commented:
here is .aspx code

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
            DataSourceID="otze" AllowPaging="True" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" Font-Names="Verdana" Font-Size="10pt" ForeColor="Black" GridLines="Vertical">
            <Columns>
                <asp:CommandField ButtonType="Button" ShowSelectButton="True" />
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
                    SortExpression="ID" />
                <asp:BoundField DataField="Status_Zahtjeva" HeaderText="Status_Zahtjeva" SortExpression="Status_Zahtjeva" />
                <asp:BoundField DataField="Prezime_ime" HeaderText="Prezime_ime" SortExpression="Prezime_ime" />
            </Columns>
            <RowStyle BackColor="#F7F7DE" />
            <FooterStyle BackColor="#CCCC99" />
            <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
            <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
        <asp:SqlDataSource ID="otze" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
            SelectCommand="SELECT">
        </asp:SqlDataSource>
    
    </div>
        <br />
        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
            DataSourceID="rearga" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" Font-Names="Verdana" Font-Size="10pt" ForeColor="Black" GridLines="Vertical">
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
                    SortExpression="ID" />
                <asp:BoundField DataField="Status_Zahtjeva" HeaderText="Status_Zahtjeva" SortExpression="Status_Zahtjeva" />
                <asp:BoundField DataField="Prezime_ime" HeaderText="Prezime_ime" SortExpression="Prezime_ime" />
                <asp:BoundField DataField="Ime_racunara" HeaderText="Ime_racunara" SortExpression="Ime_racunara" />
                <asp:BoundField DataField="TE_Cms" HeaderText="TE_Cms" SortExpression="TE_Cms" />
                <asp:BoundField DataField="TE_Cmswebapp" HeaderText="TE_Cmswebapp" SortExpression="TE_Cmswebapp" />
                <asp:BoundField DataField="TE_Arms" HeaderText="TE_Arms" SortExpression="TE_Arms" />
                <asp:BoundField DataField="TE_Irms" HeaderText="TE_Irms" SortExpression="TE_Irms" />
                <asp:BoundField DataField="TE_Atm" HeaderText="TE_Atm" SortExpression="TE_Atm" />
                <asp:BoundField DataField="TE_rtpsWP" HeaderText="TE_rtpsWP" SortExpression="TE_rtpsWP" />
                <asp:BoundField DataField="TE_IssuerWP" HeaderText="TE_IssuerWP" SortExpression="TE_IssuerWP" />
                <asp:BoundField DataField="TE_ClearingWP" HeaderText="TE_ClearingWP" SortExpression="TE_ClearingWP" />
                <asp:BoundField DataField="TE_Vci" HeaderText="TE_Vci" SortExpression="TE_Vci" />
            </Columns>
            <RowStyle BackColor="#F7F7DE" />
            <FooterStyle BackColor="#CCCC99" />
            <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
            <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
        <asp:SqlDataSource ID="rearga" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
            SelectCommand="SELECT">
            <SelectParameters>
                <asp:ControlParameter ControlID="GridView1" Name="ID" PropertyName="SelectedValue" />
            </SelectParameters>
        </asp:SqlDataSource>
    </form>
</body>
</html>

Open in new window

I solve this problem by using checkboxes instead of data load in gridview columns.It is much easier and have no problem like with girdview columns.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial