Hiding GridView Columns

by | 21 Apr 2013

I desperately needed to hide/show some columns of my GridView since I was showing the same user control to different groups of users and they didn’t need to see the same information. The columns’ property Visible takes Boolean value true/false but doesn’t take the value of a user function (throws error). Therefore, I searched and found alternate suggestion of hiding columns in the code view by using the columns’ indexes; like the following:

  grid0.Columns[0].Visible = false;


However, this approach has problem because if the column order is changed or a column is deleted then it will not work properly. As a result, I needed codes to be able to use the name/header text of the column to hide it. With the help of one of my colleagues we were able to manipulate some code found in online to do our job. Here it is:

       
       Common.ShowThisColumn("Command", gvAttachments, true);//default is hide

        public static void ShowThisColumn(string columnName, GridView gv, bool show) // can be reused for any GridView
        {
            int index = GetGridViewColumnIndex(gv.Columns, columnName);
            if (index > -1)
                gv.Columns[index].Visible = show;
        }

        public static int GetGridViewColumnIndex(DataControlFieldCollection columns, string DataColumnName)
        {
            foreach (DataControlField field in columns)
            {
                if (field is System.Web.UI.WebControls.BoundField)
                {
                    if (((BoundField)field).DataField == DataColumnName)
                        return columns.IndexOf(field);
                }
                else if (field is System.Web.UI.WebControls.TemplateField)
                {
                    if (((TemplateField)field).HeaderText == DataColumnName)
                        return columns.IndexOf(field);
                }
            }
            return -1;
        }

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

Other Posts

Amazing

Recently, I had an opportunity to work with a vision impaired employee of our agency. I was amazed by his ability to work almost at the same speed and accuracy of a sighted person. Of course he uses a special software in his computer but he mastered the skills of...

In Loving Memory of Michael Brown: A Neighbor’s Unforgettable Smile

I was shocked to hear that our neighbor, Michael Brown, passed away on May 2, 2024, and even more so to find out only after thirty days had passed. I had gone to see him because I noticed his house was up for sale. I assumed he was moving to Florida or somewhere else...

Displaying Formatted Text

Dynamically displaying formatted text is a pain. A label control can’t keep the format. People suggest to use multiline texbox but that doesn’t look good in a web page for announcement. Some suggests to use RichTextBox but that requires a great deal of coding. I...

No Visa Required

All of a sudden my wife decided to visit her parents in Bangladesh. She would take our little one with her. This is his first visit to Bangladesh, so his passport needs Bangladeshi visa. Fortunately, Bangladesh government provides “No Visa Required” (NVR) stamp on the...

Family Vacation 2022

We (my parents, two brothers with their families and I with my family) went on a family vacation in upstate (Peck’s Lake) NY this year. My only sister and her family live a few thousand miles away from us, so she couldn’t join us this year. The vacation house was...

Obnoxious Driver

Around noon on Saturday 13 July 2019 I was driving towards Queens, NYC on Brooklyn Queens Expressway (BQE) and little before exit 23 (still in Brooklyn) I was trying to go to the right lane but a car came too fast and too close to mine in the right lane, so I went...

Customer Service

I am pretty sure you deal with customer service representatives if you live in the USA. Most of the times it is their fault that you have to call them, for example, you bought something online but they sent a wrong/bad item, they charged something you don’t recognize,...