Dynamic Gridview using C#

(ASP.NET)


Hi,
In this article i am creating dynamic gridview using c# language on the click of button.

I populate the data table by fetching the data from database table. Then i iterate the  data table to
get the columns, for each column i create the template field with text box then add it to the dynamic gridview.

.aspx page

  <div>
        <asp:Button ID="btn_products" OnClick="btn_products_Click" runat="server" Text="Products Gridview" />&nbsp;&nbsp;
        <asp:Button ID="btn_customers" OnClick="btn_customers_Click" runat="server" Text="Employees Gridview" />
    </div>


Codebehind file:-

   protected void btn_products_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Northwindconstring"].ToString());

                sqlcon.Open();

                string query = "select * from Products";
                SqlCommand cmd = new SqlCommand(query, sqlcon);
           
                SqlDataReader rdr = cmd.ExecuteReader();

                DataTable dt = new DataTable();

           

                dt.Load(rdr);

                sqlcon.Close();

                dynamicGridview(dt);


            }
            catch (Exception)
            { }
        }

        protected void btn_customers_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Northwindconstring"].ToString());

                sqlcon.Open();

                string query = "select * from Employees";
                SqlCommand cmd = new SqlCommand(query, sqlcon);

                SqlDataReader rdr = cmd.ExecuteReader();

                DataTable dt = new DataTable();

          
                dt.Load(rdr);

                sqlcon.Close();

                dynamicGridview(dt);

            }
            catch (Exception)
            { }
        }

        private void dynamicGridview(DataTable dt)
        {
            try
            {
                GridView gv = new GridView();
                gv.AutoGenerateColumns = false;

                foreach (DataColumn column in dt.Columns)
                {
                    TemplateField TmpCol = new TemplateField();
                    TmpCol.HeaderText = column.ColumnName;
                    gv.Columns.Add(TmpCol);
                    TmpCol.ItemTemplate = new TemplateHandler();
                }

             
            
                gv.DataSource = dt;
                gv.DataBind();
                Form.Controls.Add(gv);
            }
            catch (Exception)
            { }
        }
    }

    public class TemplateHandler : ITemplate
    {
        string columnText = "hello";

        void ITemplate.InstantiateIn(Control container)
        {
            TextBox txtbx = new TextBox();
            txtbx.ID = "txt_"+columnText;
            txtbx.Text = columnText;
            container.Controls.Add(txtbx);
        }

      
     
    }


Ratings


Average Rating: 0.00 by 0 users
Giri Prasad
6/10/2013
6/10/2013
Download PDF

Comments