【发布时间】:2011-08-03 05:41:45
【问题描述】:
我有一个使用数据集填充数据的 Gridview。
我还有一个 DropDownlist,它是 TemplateField 的 EditTemplate。
我想将它绑定到数据集,以便它可以从中填充数据。我搜索了它,但它似乎不起作用。我是新手。如果不是代码,一些帮助我获得一个很好的教程。
这是我的代码 sn-p:
`
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;
}`
注释的代码是我试过的。
谢谢
【问题讨论】: