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

  • #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

    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

    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

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

    protected void GV_OnROwDataBound(object sender,GridviewRowEventArgs e)
    string val=((Lable)e.Row.FindControl("lblval")).TExt;
    if(val== string.Empty || val=="")


    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.

    Blog :

  • #736189
    You can always

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

    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