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++)
{
DataRow drow = dt.NewRow();
drow[NAME] = ds1.Tables[0].Rows[nIndex][0].ToString();
dt.Rows.Add(drow);
}
}
foreach (DataColumn col in dt.Columns)
{
BoundField bfield = new BoundField();
bfield.DataField = col.ColumnName;
bfield.HeaderText = col.ColumnName;
GridView1.Columns.Add(bfield);
}
for (int k = 1; k <= days; k++)
{
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.