Search code examples
c#asp.netcheckboxdatagridviewcolumn

show/hide grid-view column by column name


I'm creating a web project in Visual Studio 2012 using C# which fetch data from database and shows in a grid-view. There are many number of columns in the database, which shows on the grid-view as it is. I want to make an option for the user to eliminate unwanted columns using check-boxes and after checking, on a button click it must update.

I found how to hide a column by its column name. I need to find out how can I display it with the column name.

if (CheckBox3.Checked)
{
    dt.Columns.Remove("Site_name");
    GridView1.DataSource = dt;
    GridView1.DataBind();
}
else
{
    dt.Columns.Show("Site_name"); //I want a code to display, using column name.
}

Solution

  • Well I found a very simple way show and hide a particular column using check-box on a button click.

     if (CheckBox3.Checked == false)
     {
        dt.Columns.Remove("Site_name");
        GridView1.DataSource = dt;
        GridView1.DataBind();
     }