If you want to hide the column by its name, not its index in the GridView. After creating a DataTable or Dataset, you need to find the index of the column by its name, and then save the index in a global variable, such as ViewStae, Session, etc., and then call it in a RowDataBound, for example, an example:
string headerName = "Id"; DataTable dt = .... ; for (int i=0;i<dt.Columns.Count;i++) { if (dt.Columns[i].ColumnName == headerName) { ViewState["CellIndex"] = i; } } ... GridView_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Footer) { int index = Convert.ToInt32(ViewState["CellIndex"]); e.Row.Cells[index].Visible = false; } }
Parsa
source share