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

Windows Phone Emulator

Recently, I started to learn about creating applications for windows phone. However, every time I tried to run my application I started to get the following message: Clicking “Retry” worked but it was annoying, so I looked for a permanent solution. I found it and the...

My Grandmother

My mother’s mother is an unfortunate woman. She lost a daughter at its infancy, a son died at his childhood, a daughter died leaving three little kids – she raised the youngest one of them. Her husband died in her mid forties. Never married again she lived most of her...

Power of Social Media

There is no doubt social media is very powerful - Turkish President was able to thwart a coup by posting his video message in twitter. Recently, I was able to use it for my benefit too. I fought with Whirlpool's supervisor (over the phone) to fix my microwave oven but...

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

AT&T Cheated Me

I complained many times against AT&T, still stayed with them because they gave me good prices for phones. This time they lied to me and I believe I won’t stay with them after my contract is over unless they give me what they promised. First, they sold me Samsung...

No Juice; Water please

I had to go to doctors' office today and I found the following article on their board https://www.instagram.com/p/BvUke2LHoMz/?utm_source=ig_web_copy_link It says, " The non-profit consumer research and advocacy group tested 45 fruit juices (apple, grape, pear and...

Fun, Waves, and Celebrations: Our Havre de Grace Vacation

Every year, my family and I eagerly anticipate our family vacation, a time to unwind and make special memories together. This year held extra significance as we gathered in Havre de Grace, a city in Maryland, to celebrate my parents' remarkable 50th wedding...