In case we have to give ability to the user to select the columns that should appear in the GridView, we can follow this approach.

Sometimes due to large number of columns to be displayed on the page in the GridView or give a precise look to the data in GridView, we need to show only those columns in the GridView that is selected by the user. In this article we are going to learn how to dynamically generate GridView columns based on user selection.

In the above code snippet, we have a CheckBoxList control that lists all the columns of my database table. Apart from this, we have a Show button and a GridView. After selecting the columns from the CheckBoxList control user click on the Show button and accordingly the GridView columns are displayed on the page. Notice that AutoGenerateColumns property is set to false in the GridView.

In the above code snippet, we have called BindTableColumns() method that uses sp_columns stored procedure (no need to create it, it comes by default with SQL Server) to retrieve the column names of the database table and populates into the CheckBoxList control (In case you have column names coming from more than one database table, you will need to add them one by one into the CheckBoxList control).

When user click on Show button after selecting the columns from the CheckBoxList control, we are calling ShowGrid server side method. In this method we are iterating through the columns of the GridView and if selected, adding into the GridView column collection. At last we are calling the GetData method that fetches the records from the database and populates into the GridView.

As we have specified AutoGenerateColumns to false so whatever columns will be added to the GridView will only be displayed on the page.

Hope this article was useful. Thanks for reading and subscribe for the RSS feed to get forthcoming articles alert directly into your inbox.

@table_name is used to provide the name of the database table whose fields I am going to populate into the the CheckBoxList control. In this case "PersonalDetail" is the name of database table whose columns have been populated into the CheckBoxList control.

Thanks for the article. I am having difficulty in getting the checklistbox to populate, nothing shows up. I modifed the connection string for our connection and the table name for our database. Any help would be appreciated. Here is the code that I am using:

Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
Response.Write("hello")
BindTableColumns()
End If
End Sub
Private Sub BindTableColumns()
Dim table As New DataTable()
Using conn As New SqlConnection(_connStr)
Using cmd As New SqlCommand("sp_columns", conn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@table_name", "Mem")
Response.Write(cmd)
' get the adapter object and attach the command object to it
Using ad As New SqlDataAdapter(cmd)
' fire Fill method to fetch the data and fill into DataTable
ad.Fill(table)
End Using
chkFields.DataSource = table
chkFields.DataBind()
End Using
End Using
End Sub
Private Sub GetData()
Dim table As New DataTable()
' get the connection
Using conn As New SqlConnection(_connStr)
' write the sql statement to execute
Dim sql As String = "SELECT * FROM Mem ORDER By [Last Name]"
' instantiate the command object to fire
Using cmd As New SqlCommand(sql, conn)
' get the adapter object and attach the command object to it
Using ad As New SqlDataAdapter(cmd)
' fire Fill method to fetch the data and fill into DataTable
ad.Fill(table)
End Using
End Using
End Using
' specify the data source for the GridView
GridView1.DataSource = table
' bind the data now
GridView1.DataBind()
End Sub

Protected Sub ShowGrid(sender As Object, e As EventArgs)
For Each item As ListItem In chkFields.Items
If item.Selected Then
Dim b As New BoundField()
b.DataField = item.Value
b.HeaderText = item.Value
GridView1.Columns.Add(b)
End If
Next
Me.GetData()
End Sub

hi Sheo Narayan
this is a good article .For the resultant gridview i tried to set pageindexchanging event.when i click on the page index which is visible beneath the gridview the gridview is disappearing. Please help me in this regard.
This is the code i am trying.
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" EnableModelValidation="True"
AllowPaging="true" onpageindexchanging="GridView1_PageIndexChanging"
EnableViewState="False"/>