Click here to Skip to main content
15,844,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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)

    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";
        SqlDataAdapter da = new SqlDataAdapter(sql, sqlconn);
        DataSet ds = new DataSet();
        GridView1.DataSource = ds.Tables[0];

//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));

        //Create an Fluency column for adding to the Datatable
        dcol = new DataColumn(Fluency, typeof(System.String));

        //Create an Comprehension column for adding to the Datatable
        dcol = new DataColumn(Comprehension, typeof(System.String));

        //Create an Overall column for adding to the Datatable
        dcol = new DataColumn(Overall, typeof(System.String));

        //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.

        //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)


        //Initialize the DataSource
        GridView1.DataSource = dt;
        //Bind the datatable with the GridView.
        //for (int i = 0; i <= GridView1.Columns.Count; i++)
        //{ if (i < 6) GridView1.Columns.RemoveAt(i); }
Updated 17-Oct-13 3:22am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900