You must Sign In to post a response.
  • Category: ASP.NET

    Hide gridview columns

    Hi all,

    Am using gridview with binding values.

    I have lot of values to bind.

    For Eg:
    --------

    A |B |C |D |E |F |G| H
    ----------------------
    1. 1 |2 | 3| | | |6| 7
    -----------------------
    2. 5 |7 | 9|10| | | | 8

    i need to hide null columns like in 1st row D,E,F and 2nd row E,F,G

    How to hide

    Give sample code

    Thanks
  • #736098
    The way you want is not a column hiding, you can not hide column ROW WISE.
    if you want to hide values ROW WISE then you need to use RowDataBound_event in gridview
    In this event you can check row index according assign empty value to cell
    see below code snippet

    void gridview1_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
    // make cell empty for specific rows.
    if(e.Row.RowIndex == 2)
    {
    e.Row.Cells[1].Text = "";
    e.Row.Cells[2].Text = "";
    e.Row.Cells[3].Text = "";
    }
    else if(e.Row.RowIndex == 3)
    {
    e.Row.Cells[4].Text = "";
    e.Row.Cells[5].Text = "";
    }

    }
    }


    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #736100
    If the columns contains Header you can not do so as in forst row it might be NULL but in the next row it contains some values.

    Thus you can not be sure for Entire Columns.

    Instead Display values Row Wise and Hide them if they are NULL.

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #736105
    Hi,

    Gridview will have RowDataBound event, which will be executed for each row. But as per your requirement it is not possible because in gridview you can hide total column but not partial column.

    I think, it is better you can generate the table dynamically from your code based on your requirement. As you are dynamically generating, you can hide what is not needed and show what is needed.

    protected void gridview1_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
    //Your logic
    }
    }

  • #736173
    Hi,

    If you want to hide Gridview columns when it was empty then refer below sample


    protected void GV_OnROwDataBound(object sender,GridviewRowEventArgs e)
    {
    if(e.Row.RowType==DAtaControlRowType.DAtaRow)
    {
    string val=((Lable)e.Row.FindControl("lblval")).TExt;
    if(val== string.Empty || val=="")
    {
    ((Lable)e.Row.FindControl("lblval")).Enabled=false;
    }

    }
    }


    Try something like above to achieve your goal.

    Hope this will help you to resolve the issue.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #736189
    Hi,
    You can always


    GridView2.Columns[0].Visible = false;//change the index based on the column you wanted to hide.
    GridView2.Columns[1].Visible = false;


    Regards,
    Asheej T K

  • #736212
    That is bad idea to hide columns, If the entire rows are do not contain the value of particular column then you can hide that columns easily otherwise it does not have the meaningful...


  • Sign In to post your comments