Dharmendra Kumar

Dharmendra Kumar

  • NA
  • 83
  • 3.2k

Dynamic templatefield in gridview

Oct 17 2013 8:23 AM
Am unable to generate a gridview having some Boundfields columns with some Dynamic Templatefield columns generated on behalf of user's input that how many columns he specifies.
Please Help me...
below is themodified code from the article
How to create template columns dynamically in a grid view

protected void Button2_Click(object sender, EventArgs e)
    {
        loadDynamicGridWithTemplateColumn();
    }
 
    private void loadDynamicGridWithTemplateColumn()
    {
        #region Code for preparing the DataTable
 
        //Create an instance of DataTable
        DataTable dt = new DataTable();
 

 

 
        sqlconn = new SqlConnection(connstring);
        string sql = "SELECT ROW_NUMBER() OVER(ORDER BY ssm.stu_name) AS 'row_num', stu_name, pk_student_id FROM SM_STUDENT_MASTER ssm WHERE ssm.current_class = '" + select_class.SelectedValue.ToString() + "' AND ssm.current_section = '" + select_section.SelectedValue.ToString() + "' AND ssm.stu_status <> 0 ORDER BY stu_name";
        sqlconn.Open();
        SqlDataAdapter da = new SqlDataAdapter(sql, sqlconn);
        DataSet ds = new DataSet();
        da.Fill(dt);
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
        sqlconn.Close();
 

 

//This is the dynamic portion which will be given by the user..

        //Create an Pronunciation column for adding to the Datatable
        DataColumn dcol = new DataColumn(Pronunciation, typeof(System.String));
        dt.Columns.Add(dcol);
 
        //Create an Fluency column for adding to the Datatable
        dcol = new DataColumn(Fluency, typeof(System.String));
        dt.Columns.Add(dcol);
 
        //Create an Comprehension column for adding to the Datatable
        dcol = new DataColumn(Comprehension, typeof(System.String));
        dt.Columns.Add(dcol);
 
        //Create an Overall column for adding to the Datatable
        dcol = new DataColumn(Overall, typeof(System.String));
        dt.Columns.Add(dcol);
 
        //Now add data for dynamic columns
        //As the first column is auto-increment, we do not have to add any thing.
        //Let's add some data to the second column.
        for (int nIndex = 0; nIndex < 10; nIndex++)
        {
            ////Create a new row
            //DataRow drow = dt.NewRow();

            ////Initialize the row data.
            //drow[Pronunciation] = "";
            //drow[Fluency] = "";
            //drow[Comprehension] = "";
            //drow[Overall] = "";

            ////Add the row to the datatable.
            //dt.Rows.Add(drow);
        }
        #endregion
 
        //Iterate through the columns of the datatable to set the data bound field dynamically.
        //foreach (DataColumn col in dt.Columns)
        
        for (int i = 0; i <= dt.Columns.Count; i++)
        {
            if (i < 7)
            {
                DataColumn col1 = dt.Columns[i];
                
                //Declare the bound field and allocate memory for the bound field.
                TemplateField bfield = new TemplateField();
 
                //Initalize the DataField value.
                bfield.HeaderTemplate = new GridViewTemplate(ListItemType.Header, col1.ColumnName);
 
                //Initialize the HeaderText field value.
                bfield.ItemTemplate = new GridViewTemplate(ListItemType.Item, col1.ColumnName);
 
                //Add the newly created bound field to the GridView.
                
                //if (dt.Columns.Count < 7)
                {
                    GridView1.Columns.Add(bfield);
                }               
            }
            
        }
 
        //dt.Columns.Remove("row_num");

        //Initialize the DataSource
        GridView1.DataSource = dt;
        //Bind the datatable with the GridView.
        GridView1.DataBind();
        
        //for (int i = 0; i <= GridView1.Columns.Count; i++)
        //{ if (i < 6) GridView1.Columns.RemoveAt(i); }
    }