I have a Gridview which populates data witht a Dataset.
I also have a DropDownlist which is a an EditTemplate
of a TemplateField
.
I want to bind it to the dataset so that it can populate data from it.I searched it but it doesn't seem to work.I'm new to this. If not the code,Some help me to get hands on a good tutorial.
Heres my code snippet:
`
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false) {
BindGrid();
}
}
private void BindGrid() {
//Get dataset
//bind
DataSet ds = new DataSet("Employees");
SqlConnection con = new SqlConnection("Password=admin;User ID=admin;Initial Catalog=asptest;Data Source=dbsvr");
SqlDataAdapter da = new SqlDataAdapter("select * from employees", con);
da.Fill(ds);
gvEmp.DataSource = ds;
gvEmp.DataBind();
}
protected void gvEmp_RowEditing(object sender, GridViewEditEventArgs e)
{
gvEmp.EditIndex = e.NewEditIndex;
BindGrid();
BindDropDown();
}
private void BindDropDown() {
//DataSet ds = new DataSet("Employees");
//SqlConnection con = new SqlConnection("Password=priyal;User ID=priyal;Initial Catalog=asptest;Data Source=dbsvr");
//SqlDataAdapter da = new SqlDataAdapter("select deptno from employees", con);
//da.Fill(ds);
//gvEmp.DataSource = ds;
//gvEmp.FindControl("ddlDept").DataBind();
}
protected void gvEmp_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
//this means that no index is selected
gvEmp.EditIndex = -1;
}`
The commented code is what I tried.
Thanks
I think you were close - try this in the BindDropDown() method:
You need to locate the control in the GridView, cast it to the correct type of control (in this case, DropDownList) and then perform actions on it.
Try this.
Unless you pass the rows
DropDownList
as a parameter, how would yourBindDropDown
recognize whichDropDownList
to bind to?