1

I have a datagridview name is "grdShowDeatils", a class with name "objShowCampaignStats" having method which returns a DataTable name of method is "GetCapmaignsStatsDetails()"

{
grdShowDeatils.DataSource = AnyDataTable;
}

My Problem is dataTable has three columns , i want to hide 2nd column at run time it should not be visible for user. How i can do this ?

AHMAD SUMRAIZ
  • 535
  • 8
  • 21
  • try the answer to this question http://stackoverflow.com/questions/3819247/gridview-hide-column-by-code?rq=1 – energ1ser Feb 05 '14 at 06:18
  • well you can just say the other to columns should be collapsed take a look here this might help http://stackoverflow.com/questions/3819247/gridview-hide-column-by-code http://stackoverflow.com/questions/5376278/how-to-hide-a-column-gridview-but-still-access-its-value http://www.codeproject.com/Questions/256498/Gridview-Column-make-visiable-true-and-false – Wolf Feb 05 '14 at 06:23

4 Answers4

5

Use this

{
    grdShowDeatils.DataSource = objShowCampaignStats.GetCapmaignsStatsDetails();
    grdShowDeatils.Columns[1].Visible = false;
}
AHMAD SUMRAIZ
  • 535
  • 8
  • 21
3

You can write the following code

{    
    grdShowDeatils.DataSource = objShowCampaignStats.GetCapmaignsStatsDetails();
    grdShowDeatils.Columns[3].Visible = false;
}

or

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        e.Row.Cells[0].Visible = false;
    }
Nagaraj S
  • 13,316
  • 6
  • 32
  • 53
1

Suppose your DataTable returns below three columns ,

Name , Address , Phone No. And you want to hide Address Column

At .aspx page you can bind DataGridView as per the below,

<asp:DataGridView ID ="grdShowDeatils" runat="server">
 <Columns>
  <asp:BoundColumn HeaderText="Name" DataFeild="Name"/>
  <asp:BoundColumn HeaderText="Address" DataFeild="Address" Visible = "False"/>
  <asp:BoundColumn HeaderText="Phone No." DataFeild="PhoneNo"/>
 </Columns>
</asp:DataGridView>

OR

You can code at RowDataBound event of DataGridView as per below,

protected void grdShowDeatils_RowDataBound(object sender, GridViewRowEventArgs e)
{
   e.Rows.Cells[1].Visible = false;
}
Lajja Thaker
  • 2,031
  • 8
  • 33
  • 53
0

I assume this is asp.Net Web Forms--can't you just set grdShowDeatils.autoGenerateColumns = "false" and then just specify the 1st and 3rd columns (via the designer or programmatically)?