Ramani Sandeep's Blog

DotNetting – Fast , Easy Way of Developing Applications

How to hide Gridview column programmatically?

Posted by Ramani Sandeep on April 7, 2009

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:

        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 this will help you !!!

Happy Programming

Advertisement

52 Responses to “How to hide Gridview column programmatically?”

  1. kaka said

    Thanks for sharing this.
    I’ve been looking how to hide the column… and it works! ^_^

    • Karthick said

      Its absolutely working

  2. Greg said

    Thanks a lot!!!!
    Great explanation!
    Works like a charm!

  3. Shivraj said

    coollllllllllllllllllllllllll

    Solution
    Greattttttttttt Job ramanisandeep

  4. Ajit said

    Hi,

    How to hide a column ( Primary key) in Data Grid View if they are later used for editing purpose on click of context menu.

  5. Michael said

    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.

    • ramanisandeep said

      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 false..it means it does get rendered so you will not have it value.. so try hidden field..it will work..

  6. [...] How to hide Gridview column programmatically? [...]

  7. Sunit said

    Great post. Thanks alot

  8. Terry said

    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?

  9. Terry said

    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?

    • Ramani Sandeep said

      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

      • Terry said

        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.

  10. Tarun said

    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?
    GridView1.DataBind();

    02 if (GridView1.Columns.Count > 0)

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

  11. sunil said

    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

    • Ramani Sandeep said

      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!!!

  12. Great post. Thanks alot

  13. Osman said

    Thank you

    • Sourabh S. Malani said

      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

  14. Fx15 said

    thanks reply

  15. Shreya said

    Thanks a lottt………….!!!!!
    It worked….!!!

  16. Minh Nhat Hoang said

    Thanks so much, now I can complete my Assignment :)

  17. kalai said

    I use this code successfully hide column but my page index also hided… So there may be a issue in this code…

  18. dinesh said

    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

  19. John giblin said

    anyway of using a name to get the column?

    • Ramani Sandeep said

      not getting what you are asking?

  20. Thanks a lot.Matter of fact it makes sense to keep checking out with cells at autogeneratecolumns true in case of data binding!

  21. Tiago Bento said

    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!

  22. bonrc said

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

  23. SIVANEE said

    I want to hide only the third column of the grid view

  24. Ruby said

    Great ! this really helped me !!! Thanks

  25. sajid said

    Excellent !

  26. Thank you very much!

  27. Shekar said

    Thank You… I was looking exactly for this…

  28. Thanks a lot…..

  29. suryanaryana said

    thank u man

  30. Serkan said

    Serkan…

    [...]How to hide Gridview column programmatically? « Ramani Sandeep's Blog[...]…

  31. thanks buddy it’s really good.

  32. Pravin Varade said

    How to hide column in nested gridview asp.net?

    My second gridview is in itemtemplate of first gridview.

    • Ramani Sandeep said

      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 !!!

  33. msbuzzz said

    Great Stuff!!

  34. Pravin Varade said

    Thanks Boss!!!… Great… This Solution solves my problem.

    • Ramani Sandeep said

      Anytime pravin… I am glad to know this…

  35. NARAYAN said

    Cooooooooooooollllllllll

  36. NARAYAN said

    Simple and great. Thanks

  37. Arthur Savage said

    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;
    }

  38. krishna said

    when i scroll my scrollbar Div half way down , then trigger a reload , i want it tries to find the last scroll position. instead of go back to the top

  39. Rutika Patel said

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

    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

  40. I believe other website proprietors should take this site as an example , very clean and good user friendly design and style .

  41. Laura said

    I know this post is very old, but you have no idea how this helped me! thank you so much!

  42. linkonbaruaiiuc said

    Many many many tx for your post

    • Pravin Varade said

      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?

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

 
Follow

Get every new post delivered to your Inbox.

Join 37 other followers