Click here to Skip to main content
15,891,253 members

how to get value of dynamically created dropdown using template in gridview

Mitu Machhi asked:

Open original thread
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.
Tags: C#, Gridview

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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