I created my own solution based on the things that I read here and in other topics .. here it goes:
public void HideColumnByName(GridView grid, string header) { if (grid.HeaderRow.HasControls()==true) { for (int i = 0; i < grid.HeaderRow.Cells.Count; i++) { if (grid.HeaderRow.Cells[i].Text == header) { foreach (GridViewRow row in grid.Rows) { row.Cells[i].Visible = false; grid.HeaderRow.Cells[i].Visible = false; } } } } }
this method directly hides both headers and column cells, the header name (or column name) is the string parameter passed to my method (the 'header' parameter). Then the "HideColumnByName" method is called from the "DataBound" event of my gridview. Just. Hope this helps! It really helped me! :)
source share