[Last Call] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2009-04-28
Medium Priority
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
  • 2
LVL 14

Expert Comment

ID: 24257080

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

ID: 24265295
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

GiftsonDJohn earned 2000 total points
ID: 24266579
I have modified the code. Is it helping?

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Integration Management Part 2
Loops Section Overview
Suggested Courses
Course of the Month17 days, 22 hours left to enroll

830 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