数据绑定的DropDownList的GridView的EditItemTemplate里面 [英] DataBinding DropDownList inside GridView EditItemTemplate
问题描述
我有一个GridView其中填充数据witht的数据集。
我也有一个DropDownList这是一个 EditTemplate
的模板列
。
我想将它绑定到数据集,这样从它。我找来找去就可以填充数据,但它似乎并不work.I'm新本。如果不是code,有的帮我拿到手的一个很好的教程。
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.
我的继承人code片断:
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;
}`
本评论code是什么我试过了。
The commented code is what I tried.
感谢
推荐答案
试试这个。
protected void gvEmp_RowEditing(object sender, GridViewEditEventArgs e)
{
DemoGrid.EditIndex = e.NewEditIndex;
BindGrid();
DropDownList dropdown = gvEmp.Rows[e.NewEditIndex].FindControl("DropDownList1") as DropDownList;
BindDropDown(dropdown);
}
private void BindDropDown(DropDownList temp)
{
string connectionString = "Password=priyal;User ID=priyal;Initial Catalog=asptest;Data Source=dbsvr";
string query = "select deptno from employees";
DataSet dataset = new DataSet("Employees");
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection))
{
adapter.Fill(dataset);
}
}
temp.DataSource = dataset;
temp.DataTextField = "deptno";
temp.DataValueField = "deptno";
temp.DataBind();
}
除非你通过行的DropDownList
作为参数,如何将你的 BindDropDown
识别哪些 DropDownList的
绑定到?
Unless you pass the rows DropDownList
as a parameter, how would your BindDropDown
recognize which DropDownList
to bind to?
这篇关于数据绑定的DropDownList的GridView的EditItemTemplate里面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!