Click here to Skip to main content
15,886,199 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
C#
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); }
    }
Posted
Updated 17-Oct-13 2:22am
v3

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