Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6725217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:46:53+00:00 2026-05-26T09:46:53+00:00

I am trying to traverse the gridview columns in ASP.net web application, using the

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:46:53+00:00Added an answer on May 26, 2026 at 9:46 am

    I think u are trying to hide column based on column title so please try following code,

    // 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
    foreach (var col in gvEmployees.Columns)
    {
      if (col.Text.ToLower().Trim() == name.ToLower().Trim())
      {
          // hiding the column from the grid view.
          col.Visible = false;
      }
    }
    

    Update

    you can also write following code:

    string name = "First Name";// Column name supposed to hide
    for (int i = 0; i < gvEmployees.Columns.Count; i++)
    {
        if (gvEmployees.Columns[i].Text.ToLower().Trim() == name.ToLower().Trim())
        {
            gvEmployees.Columns[i].Visible = false;
        }
    }
    

    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.

    int index = 0;
    bool hidden = false;
    protected void gvEmployees_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if(hidden)
        {
             e.Row.Cells[index].Visible =  false;
             return;
        }
    
        string name = "First Name";// Column name supposed to hide
        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            if (e.Row.Cells[i].Text.ToLower().Trim() == name.ToLower().Trim())
            {
                e.Row.Cells[i].Visible = false;
                hidden = true;
                index = i;
                break;
            }
        }
    }
    

    Update when using auto generated columns, (for multi-column version)

    Using the rowdatabound event and hide the cell (column) when the row is bound.

    List<int> indexes = new List<int>();
    bool hidden = false;
    List<string> names = new List<string>();
    protected void gvEmployees_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if(hidden)
        {
             foreach(int index in indexes)
             {
                 e.Row.Cells[index].Visible =  false;
             }
        }
    
        // start - Column names supposed to hide
        // Building the list of column names to be hidden.
        names.Add("First Name");
        names.Add("Last Name");
        names.Add("Address");
        names.Add("ID");
        // end - Column names supposed to hide
    
        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            if (names.Contains(e.Row.Cells[i].Text.ToLower().Trim())
            {
                e.Row.Cells[i].Visible = false;
                hidden = true;
                indexes.Add(i);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to traverse through an XML file using jquery mobile. My code goes
Trying to make a MySQL-based application support MS SQL, I ran into the following
I'm trying to traverse an AJAX response, which contains a remote web page (an
I'm trying to traverse a tree via TBB tasks and continuations. The code is
I am trying to traverse all directories under the current directory using a shell
I thought this would be pretty academic but its not. I'm trying to traverse
I'm trying to iteratively traverse the United States in 1 mile increments using latitude
I'm trying to write code that will traverse an undirected, unweighted graph. Essentially, the
I am trying to traverse Binary search tree with the follwoing code and my
I'm trying to write some Python code that will traverse each directory in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.