Click here to Skip to main content
12,399,179 members (56,984 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# Gridview
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 ....



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 :

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 20-Feb-13 20:33pm
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 21 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100