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...

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>...

Empathy!

On 03 November 2018 I was coming to USA from Bangladesh and I got sick in the plane. When we landed at Shannon Airport, County Clare, Ireland I started shaking in the queue to final checking before flying to JFK airport, New York, USA. I was shaking because my fever...

#whirlpoolsalescrap

Last year when we remodeled our kitchen we bought a microwave oven (over the range), dish washer and a regular oven (free standing gas range) manufactured by whirlpool. Within first week the handle of the microwave was deformed by heat. It had warranty for a year, so...

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...

Testicular Cancer

Recently, I took a agency mandated class on communication. It was a good class and the lessons learned there can be used for effective communication in work or in personal life. The instructor shared some experiences involving his client, parents, and siblings. He...

My worst experience with doctors

It was December 2015, my second son was playing with colored cotton balls and I thought I saw him putting one small cotton ball in one of his nostrils (he was 1 year+ at that time). We saw something red in his nose but we weren’t 100% sure because the inside of a nose...