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

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

প্রতারণা

ফেসবুকে আরিফ ভাইয়ের স্ট্যাটাস দেখে আমারও কিছু প্রতারণার অভিজ্ঞতা মনে পড়ে গেল… আমার গুলো অবশ্য আমেরিকাতে। বুঝতে পারছি না কারা আগে আবিষ্কার করেছে এই কৌশলগুলো? ১) ১৯৯৭ এর কথা বেশিদিন হয়নি আমেরিকাতে এসেছি। মানহাত্তানের সহ (SOHO @ Manhattan) তে একটা স্টোরে ক্যাশিয়ারের কাজ...

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

Parents

#Parents can attest to the fact that they live their kids’ lives too. Their pain & pleasure depend on the well-being of their children too…

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

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

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