Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I am using Gridview in c# for my web app. I created a Dynamically Dropdown using Dyanamically Created Template.

but i am not able to get the value of dropdown to store in DB.

So, how can i get the value of dropdown from the each column.

i am using ....



C#
public class AddTemplateToGridView : ITemplate
   {
       ListItemType _type;
       string _colName;

       public AddTemplateToGridView(ListItemType type, string colname)
       {
           _type = type;
           _colName = colname;
       }

       void ITemplate.InstantiateIn(System.Web.UI.Control container)
       {
           switch (_type)
           {
               case ListItemType.Item:

                   DropDownList ht = new DropDownList();
                   ht.ID = "ht"+_colName;
                   ht.Width = 50;
                   ht.Items.Add(new ListItem("Select", "Select"));
                   ht.Items.Add(new ListItem("P", "P"));
                   ht.Items.Add(new ListItem("A", "A"));
                   ht.Items.Add(new ListItem("H", "H"));
                   ht.Items.Add(new ListItem("S", "S"));
                   ht.Items.Add(new ListItem("L", "L"));
                   ht.DataBinding += new EventHandler(ht_DataBinding);
                   container.Controls.Add(ht);
                   break;

           }
       }

       void ht_DataBinding(object sender, EventArgs e)
       {
           DropDownList ddl = (DropDownList)sender;
           GridViewRow container = (GridViewRow)ddl.NamingContainer;

       }
   }




Function for create row and column :

C#
private void loadDynamicGrid()
    {
       
        DataTable dt = new DataTable();
        DataColumn dcol = new DataColumn(NAME, typeof(System.String));
        dt.Columns.Add(dcol);

        int year = DateTime.Now.Year;
        int month = DateTime.Now.Month;
        int days = DateTime.DaysInMonth(year, month);

        string s = "select name from empl where id='" + Session["user"].ToString() + "'";
        SqlDataAdapter ad = new SqlDataAdapter(s, c.getcon());
        DataSet ds = new DataSet();
        ad.Fill(ds);

        if (ds.Tables[0].Rows.Count != 0)
        {
            string report_person = "Select name from empl where report_to_whom='" + ds.Tables[0].Rows[0][0].ToString() + "'";
            SqlDataAdapter ad1 = new SqlDataAdapter(report_person, c.getcon());
            DataSet ds1 = new DataSet();
            ad1.Fill(ds1);




            for (int nIndex = 0; nIndex < ds1.Tables[0].Rows.Count; nIndex++) // FOR CREATE A NUMBER OF ROW 
            {
               
                DataRow drow = dt.NewRow();
              
                drow[NAME] = ds1.Tables[0].Rows[nIndex][0].ToString();
                dt.Rows.Add(drow);
            }

        }

           
        foreach (DataColumn col in dt.Columns)
        {
            //Declare the bound field and allocate memory for the bound field.
            BoundField bfield = new BoundField();

            //Initalize the DataField value.
            bfield.DataField = col.ColumnName;

            //Initialize the HeaderText field value.
            bfield.HeaderText = col.ColumnName;

            //Add the newly created bound field to the GridView.

            GridView1.Columns.Add(bfield);
        }

        for (int k = 1; k <= days; k++) // FOR CREATE A COLUMN DYNAMICALLY
        {
            
            BoundField fname = new BoundField();
            TemplateField lname = new TemplateField();
            lname.HeaderText = d + k;
            lname.ItemTemplate = new AddTemplateToGridView(ListItemType.Item, d+k);

            GridView1.Columns.Add(fname);
            GridView1.Columns.Add(lname);

        }

       
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

so, when i click on button i want to store the selected value from that Dropdown.
Posted
Updated 20-Feb-13 22:48pm
v2

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