canuckconsulting
asked on
Unable to hide gridview column using MappingType.Hidden on dataset column
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="Gr idView1_Se lectedInde xChanged" DataKeyNames="ID"
EmptyDataText="No matching items found.">
<Columns>
<asp:CommandField ShowSelectButton="True" />
</Columns>
</asp:GridView>
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.
The gridview is declared as:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
onselectedindexchanged="Gr
EmptyDataText="No matching items found.">
<Columns>
<asp:CommandField ShowSelectButton="True" />
</Columns>
</asp:GridView>
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;
cnDB.Open();
DataSet dsResults = new DataSet();
oResultsAdapter.Fill(dsResults, "Results");
oResultsAdapter.Dispose();
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"];
GridView1.DataBind();
ASKER
That pretty much sums up my problem....the System.Data.MappingType.Hi dden setting is ignored in my gridview. Regardless which columns I set to hidden they are all displayed on the gridview.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you want to hide all the columns except Lot and ID then you have to use
foreach (DataColumn c in dsResults.Tables["Results"
{
if (!c.ColumnName.Equals("Lot
c.ColumnMapping = System.Data.MappingType.Hi
}
Else if you want to hide Lot and ID column you have to use
foreach (DataColumn c in dsResults.Tables["Results"
{
if (c.ColumnName.Equals("Lot"
c.ColumnMapping = System.Data.MappingType.Hi
}