Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi All,
am working with GridView and while binding dataset to grid i am setting column mapping to MappingType.Hidden for few columns but Gridview is showing all.
am not sure what is the issue. please if anyone tell me about the issue here.

code
VB
oDs.Tables(0).Columns("id").ColumnMapping = MappingType.Hidden
GridView1.DataSource = oDs
GridView1.DataBind()


i am not designing itemTemplate because grid is on master page and multiple pages are passing distict datasets to it. so can't set columns to visible = false.
Posted

Please try like this. Hope this will help you.
C#
foreach (DataColumn dc in oDs.Tables(0).Columns)
            {
                BoundColumn dgc = new BoundColumn();
                dgc.HeaderText = dc.ColumnName;
                dgc.DataField = dc.ColumnName;
                if (dc.ColumnName.Contains("Id"))
                    dgc.Visible = false;
                GridView1.Columns.Add(dgc);
            }
 
Share this answer
 
v2
Comments
Zubair Alie 21-Feb-12 5:28am    
i resolved the issue by self but you provided much better solution than i produced
am adding my own solution in solution list. you may have a look on that too.
shaikh-adil 28-Nov-12 8:47am    
+5
VB
Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated


        For Each col As DataColumn In CType(GridView1.DataSource, _
                                            DataSet).Tables(0).Columns
        If col.ColumnMapping = MappingType.Hidden Then
                e.Row.Cells(CType(GridView1.DataSource, _
                            DataSet).Tables(0).Columns.IndexOf(col.ColumnName) + 1)
                            .Visible = False
        End If
        Next
End Sub
 
Share this answer
 
v2
Comments
beevif 21-Feb-12 5:46am    
Many thanks for the comments and your solution is good.
shaikh-adil 28-Nov-12 8:46am    
+5
Use "GridView_RowDataBound" Event.
C#
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[index].Visible = false;
}


Put the index of column you want to hide.
 
Share this answer
 
 
Share this answer
 
hi,
try this...

VB
Gridview1.Columns(1).Visible = False
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900