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

Graduation Party

On Saturday 02 July 2022, we celebrated important milestones of our children. We had four graduates this year in our family - one graduated from kindergarten, two from elementary schools, and the oldest one among them from middle school. Kids were free to spend as...

Library Time

I try to take the kids to a library once in a week to spend some times with the books and bring some books home, so they can read them during the next week. Hopefully, they will build a habit of going to the library on their own. May be one or both the kids will be...

The Curious Case of Children’s Motrin Costs

I recently needed to purchase Children’s Motrin, which I had bought from Walmart a few days ago for $7.68. However, since Walmart is a bit far from my location, I decided to compare prices online at Rite Aid and CVS. To my surprise, Rite Aid listed it for $8.79, while...

Special Someone

Though I was never cruel to anyone other than using wrong words my softer side was seldom revealed for I never met someone who cared to look under my hard surface. In fact, I never got enough love to open my heart to someone. Most of the times, I was misunderstood, so...

Analyze That

On the day of my baby boy’s birthday (last Sunday) we were eating pizzas. At one point my mother was telling me not to eat pepperoncinis that come with papa john’s pizzas because she thought it would give me gas. I ignored and ate two anyways since I never got problem...

Color of Validation Text

You may have noticed that the color of the validation text is black in framework 4.0. To make it red again I found the following solution but some users say that it doesn’t work always. <?xml version="1.0"?> <configuration> <system.web>...

Visiting Parks

During this summer I started to take my kids to different parks, so they can spend some time outside of the house and get some physical movements rather than playing video games the whole day. I take pictures every time we go outside. Also, I make videos of less than...