Unable to hide gridview column using MappingType.Hidden on dataset column

Posted on 2009-04-28
Last Modified: 2012-05-06
I am creating a c# web project with a gridview used to display data.  I do not want several of the columns in the dataset displayed but do need programmatic access to the data so am unable to set the visible attribute as this seems to unbind the data.

The gridview is declared as:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
onselectedindexchanged="GridView1_SelectedIndexChanged" DataKeyNames="ID"
EmptyDataText="No matching items found.">
      <asp:CommandField ShowSelectButton="True" />

In the page load event I have the following code to load.  The problem is all the columns are displayed in the gridview regardless of weather I set the columnmapping attribute to hidden or not.
using (MySqlConnection cnDB = new MySqlConnection("server=localhost;user id=root;password=pwd;persist security info=True;database=test"))


                MySqlDataAdapter oResultsAdapter = new MySqlDataAdapter("select * from job", cnDB);

                oResultsAdapter.SelectCommand.CommandType = CommandType.Text;


                DataSet dsResults = new DataSet();

                oResultsAdapter.Fill(dsResults, "Results");



                foreach (DataColumn c in dsResults.Tables["Results"].Columns)


                    if (!c.ColumnName.Equals("Lot") || !c.ColumnName.Equals("ID"))

                        c.ColumnMapping = System.Data.MappingType.Hidden;



                GridView1.DataSource = dsResults.Tables["Results"];


Open in new window

Question by:canuckconsulting
    LVL 14

    Expert Comment


    If you want to hide all the columns except Lot and ID then you have to use

                    foreach (DataColumn c in dsResults.Tables["Results"].Columns)
                        if (!c.ColumnName.Equals("Lot") && !c.ColumnName.Equals("ID"))
                            c.ColumnMapping = System.Data.MappingType.Hidden;

    Else if you want to hide Lot and ID column you have to use

                    foreach (DataColumn c in dsResults.Tables["Results"].Columns)
                        if (c.ColumnName.Equals("Lot") || c.ColumnName.Equals("ID"))
                            c.ColumnMapping = System.Data.MappingType.Hidden;

    Author Comment

    That pretty much sums up my problem....the System.Data.MappingType.Hidden setting is ignored in my gridview.  Regardless which columns I set to hidden they are all displayed on the gridview.  
    LVL 14

    Accepted Solution

    I have modified the code. Is it helping?

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now