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.
This article has been written based on the question asked in this forum thread.
ASPX Code
<div>Select column to show in the GridView:<asp:CheckBoxList runat="server" ID="chkFields" DataTextField="Column_name" DataValueField="Column_name" RepeatDirection="Horizontal" RepeatLayout="Flow" /></div>
<p>
<asp:Button ID="btnSub" runat="server" Text="Show" OnClick="ShowGrid" /></p><asp:GridView ID="GridView1" runat="server" EnableViewState="false" AutoGenerateColumns="false" />
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 AutoGenerateColumnsproperty is set to false in the GridView.
.NET How to Tips and Tricks comes with hundreds of ASP.NET, ASP.NET AJAX, jQuery, HTML, CSS and JavaScript Tips and Tricks.
Code behind
string _connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString; protected void Page_Load(object sender, EventArgs e) {if (!IsPostBack)} private void BindTableColumns() {
{
BindTableColumns();}DataTable table = new DataTable();} private void GetData() {
using (SqlConnection conn = new SqlConnection(_connStr))
{
using (SqlCommand cmd = new SqlCommand("sp_columns", conn))}
{
cmd.CommandType = CommandType.StoredProcedure;}
cmd.Parameters.AddWithValue("@table_name", "PersonalDetail");
// get the adapter object and attach the command object to it
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
// fire Fill method to fetch the data and fill into DataTable}
ad.Fill(table);
chkFields.DataSource = table;
chkFields.DataBind();DataTable table = new DataTable();} protected void ShowGrid(object sender, EventArgs e) {
// get the connection
using (SqlConnection conn = new SqlConnection(_connStr))
{
// write the sql statement to execute}
string sql = "SELECT AutoId, FirstName, LastName, Age, Active FROM PersonalDetail ORDER By AutoId";
// instantiate the command object to fire
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
// get the adapter object and attach the command object to it}
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
// fire Fill method to fetch the data and fill into DataTable}
ad.Fill(table);
// specify the data source for the GridView
GridView1.DataSource = table;// bind the data now
GridView1.DataBind();foreach (ListItem item in chkFields.Items)}
{
if (item.Selected)}
{
BoundField b = new BoundField();}
b.DataField = item.Value;
b.HeaderText = item.Value;
GridView1.Columns.Add(b);
this.GetData();
No comments:
Post a Comment