【问题标题】:DataBinding DropDownList inside GridView EditItemTemplateGridView EditItemTemplate 内的 DataBinding DropDownList
【发布时间】:2011-08-03 05:41:45
【问题描述】:

我有一个使用数据集填充数据的 Gridview。 我还有一个 DropDownlist,它是 TemplateFieldEditTemplate。 我想将它绑定到数据集,以便它可以从中填充数据。我搜索了它,但它似乎不起作用。我是新手。如果不是代码,一些帮助我获得一个很好的教程。

这是我的代码 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;
}`

注释的代码是我试过的。

谢谢

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    试试这个。

    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

    【讨论】:

    • 我认为 gvEmp.Rows[e.NewEditIndex].Columms[columnIndex] 可以解决问题
    • 正如他所说,他在 edititemtemplate 中有一个下拉列表,为了可读性和可维护性,最好通过 id 访问它。
    • 我认为这是我的问题所需要的。你能在这里解释一下我的问题吗? stackoverflow.com/questions/25394054/…谢谢
    【解决方案2】:

    我认为你已经接近了 - 在 BindDropDown() 方法中试试这个:

    // Set the DropDownList's DataSource (originally you were setting the GridView's DataSource
    ((DropDownList)gvEmp.FindControl("ddlDept")).DataSource = ds;
    // Call DataBind() on the DropDownList
    ((DropDownList)gvEmp.FindControl("ddlDept")).DataBind();
    

    您需要在 GridView 中找到控件,将其转换为正确类型的控件(在本例中为 DropDownList),然后对其执行操作。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-03
    • 1970-01-01
    • 2010-09-15
    • 1970-01-01
    相关资源
    最近更新 更多