How to hide Gridview column programmatically?

Question :  How to hide Gridview column programmatically?

Solution :

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:

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

Hope this will help you !!!

Happy Programming


68 thoughts on “How to hide Gridview column programmatically?

  1. Hi
    I’m filling up gridview with the data and then hide some columns.
    For one column I change header by the value from one cell from the same row. Everything goes fine but when I’m hiding a column where I just taken a text for header I’m losing a header text as well.

    I very appreciate your help.

    • if u want to retain header text then save it in hidden field.
      if you save it in header & when we hide header we make it visible means it does get rendered so you will not have it value.. so try hidden will work..

  2. Pingback: Gridview – insert, update, delete « Web Developer Friends
  3. How do you pick a certain column by name to hide, for example, I have the columns Make Model and Serial Number, how do I had the Make column?

  4. How do you pick a certain column by name to hide, for example, I have the columns Make Model and Serial Number, how do I hide the Make column?

    • Hi Terry,
      Access the gridview column by its index no like

      if Make Model is first place than
      access it with [0] , if at second place than [1] like wise you can access it.

      Hope this help

      • Is there a way to go through the grid view and get the indexes based on a name? I am adding columns based on the user selecting items in a list view. As they select the items, the columns are added to the grid view. If they select them again i would like to have the columns disappear, insted it adds them twice. If I could look though all the columns in the grid view and get the indexes then I would be able remove the colunn instead of adding it twice.

  5. 4 me following code works to hide the coloumn
    Grd_show.HeaderRow.Cells[0].Visible = false;
    foreach (GridViewRow gvr in Grd_show.Rows)
    gvr.Cells[0].Visible = false;
    as i m generating the grid at run time so the following code does not work i do not know y so?

    02 if (GridView1.Columns.Count > 0)

    03 GridView1.Columns[0].Visible = false;

  6. Hi Sandeep,

    I’ve problem in hiding a row of girdview which is inside the datalist.suppose if the gridview row contains no data then it should be visible false.can u please help me out of this

    • I am not getting meaning of this “gridview row contains no data”. why such row exist in selection of the table & can’t you supress it from the DB selection so that no other processing required…

      Hope this help!!!

    • Hello Friend,

      For hide column in gridview

      protected void Gridview1_RowCreated(object sender, GridViewRowEventArgs e)
      if (e.Row.RowType == DataControlRowType.DataRow)
      // Index of column to be hide
      e.Row.Cells[0].Visible = false;

      if(e.Row.RowType == DataControlRowType.Header)
      // Index of column to be hide
      e.Row.Cells[0].Visible = false;

      I hope this will help you.

      Thanks & Regards,
      Sourabh S. Malani

  7. if i click once a radiobutton gridview want to visible on clicking another radiobutton first grid view1 gets hide and grid view2 wants to get visible please send me source code for this

  8. Thank you very much! I´ve been trying to fix an error for a total of 3 Hours and you saved me with a couple of lines. Again, thank you!

  9. for (int i = 0; i < GridView2.Columns.Count; i++)
    if (GridView2.Columns[i].HeaderText == "Total Hours")
    GridView2.Columns[i].Visible = false;

  10. Pingback: Serkan
    • Hi Pravin, First of try to find the Gridview Contron which is in ItemTemplate. Use the following type of code :

      protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
      if (e.CommandName.Equals(“Update”))
      int index = int.Parse(e.CommandArgument.ToString());
      GridViewRow row = GridView1.Rows[index];
      GridView gvState = (GridView)row.FindControl(“GridViewID”);
      //Use the gvState Gridview and hide the columns you want to hide using above code psoted for simple grid.

      Hope this will help !!!

  11. function hideCol(columns){
    //col_num = 0 //document.getElementById(“column_numbder”).value;
    rows = document.getElementById(“GridView1”).rows;

    for(i=0; i<rows.length; i++)
    { rows[i].cells[columns].style.display="none"; }
    return true;

    function showCol(columns){
    //col_num = 0 //document.getElementById("column_numbder").value;
    rows = document.getElementById("GridView1").rows;
    for(i=0; i<rows.length; i++)
    { rows[i].cells[columns].style.display=""; }
    return true;

  12. hello sandeep
    i want to call java script function from code be-hide of

    Page.ClientScript.RegisterStartupScript(this.GetType(), “test”, string.Format(“javascript:SetCookies()”));

    i can wright as under given
    but its not working… or not call this function please tell me what is problem

    • Rdlc Report not display properly in Google Chrome but work fine in IE. How to set width in columns of table in RDLC Report which display properly in all browser?

    • if your checkbox column is first column in grid than hide the first column using my code , it will work.


  13. Hi,
    I have following requirement. Basically my gridview has to show 4 Columns Quarter1, Quarter2, Quarter3 and Quarter4. Each Quarter have 3 Periods Period1, Period2 and Period3. And Each Period have 4 weeks wk1, wk2…wk12. My Requirement is if i click on Quarter1 header it has to show the corresponding Periods with respective weeks. Then if i click on Quarter2 header it has to collapse Quarter1 data and it has to show Quarter2 data as Quarter1. like this i have to implement all the Quarters in the gridview in my web application..

    Please help me..
    Thanks in advance..

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s