I am trying to traverse the gridview columns in ASP.net web application, using the following code, However the code is not going inside the foreach loop.
// Code snippet to hide columns from a gridview named 'gvEmployees'
gvEmployees.DataSource = dvItems.ToTable();
gvEmployees.DataBind();
string name = "First Name"; //Column name supposed to hide
int index=-1;
foreach (DataColumn col in gvEmployees.Columns)
{
if (col.ColumnName.ToLower().Trim() == name.ToLower().Trim())
{
// Getting the column index if find a match
index= gvEmployees.Columns.IndexOf(col);
// Using the above index, hiding the column from the grid view.
gvEmployees.Columns[index].Visible = false;
}
}
I am trying to hide some columns from gridview.
I think u are trying to hide column based on column title so please try following code,
Update
you can also write following code:
Update when using auto generated columns (for single-column version),
You need to use the rowdatabound event and hide the cell (column) when the row is bound.
Update when using auto generated columns, (for multi-column version)
Using the rowdatabound event and hide the cell (column) when the row is bound.