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

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

National Pride vs. People’s Lives

More lives could have been saved but the Bangladeshi government reportedly refused to take assistance from some friendly countries in rescuing garment workers from collapsed “Rana Plaza” because of national pride. I am not sure where is the pride coming from. Other...

A Terrifying Night: When a Nosebleed Turned into a Nightmare

Parenting is a journey of love, joy, and challenges. One fateful night, after a trip from New Jersey, my baby boy started having a nosebleed. At first, it seemed minor, but soon after cleaning up and trying to keep his head back, things took a frightening turn. The...

Just Business

Last Saturday, there was a community fair at Max Myers playground in northeast Philadelphia. In the fair, they were giving out free foods sponsored by local politicians since election is in the horizon. Also, some companies were giving hot dogs, red Bull, etc. in...

মাততে পারেনি

যদিও শব্দটার অর্থ জানাছিল তবুও সিলেটী ভাইয়েরা যখন নিজেদের ভাষায় কথা বলেন তখন দুর্বোধ্য শোনায়। তাই যখন আমি আমার ছেলেকে আরবি পড়ার শেষে মসজিদ থেকে আনার সময় অন্য একজন অবিভাবক জিজ্ঞেস করলেন 'মাততে পারেনি' আমি বুঝতে পারিনি। দ্বিতীয়বার যখন বললেন 'ছেলে বাংলায় মাততে পারেনি'।...

My Ordinary Life

In my personal life there is everything present for me to be happy. I am happy somewhat but I don’t feel satisfied. My existence doesn’t mean much outside of my family. If I die today I will be forgotten very fast. This world will not miss anything because of my...

Gabriel García Márquez

“One Hundred Years of Solitude” by Gabriel García Márquez was one of the novels that we had to read in our undergraduate “classical literature” class (13 years ago). We read this as a class and discussed it. At the beginning, all the Spanish names in the book were...