在asp.net中的gridview中绑定下拉列表

时间:2012-05-19 06:47:59

标签: asp.net html.dropdownlistfor

HTML代码:

        <ItemTemplate>
            <asp:Literal ID="faculty" runat="server" Text='<%#Eval("facultyname")%>'>
            </asp:Literal>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:DropDownList ID="fact" runat="server">
            </asp:DropDownList>

代码背后:

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

        {
            DropDownList dropdown = GridView1.Rows[e.NewEditIndex].FindControl("fact") as DropDownList;
            facultyDal c = new facultyDal();
            dropdown.DataSource = c.show();
            dropdown.DataBind();
            dropdown.DataTextField = "facultyname";
            dropdown.DataValueField = "facultyid";

        }

例外:

对象引用未设置为对象的实例。

当我使用数据源绑定到下拉列表时,会出现上述异常,请帮助....

4 个答案:

答案 0 :(得分:1)

使用RowDataBound尝试这样做。

对于editmode中的绑定

protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow && gvCustomers.EditIndex == e.Row.RowIndex)
    {
        DropDownList ddlfaculties = (DropDownList)e.Row.FindControl("fact");
        string query = "select distinct facultyname from your-Table-Name";
        SqlCommand cmd = new SqlCommand(query);
        ddlfaculties .DataSource = GetData(cmd);
        ddlfaculties .DataTextField = "facultyname";
        ddlfaculties .DataValueField = "facultyid";
        ddlfaculties .DataBind();
        ddlfaculties .Items.FindByValue((e.Row.FindControl("faculty") as Literal).Text).Selected = true;
    }
}

用于从下拉列表中更新所选值。

protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow && gvCustomers.EditIndex == e.Row.RowIndex)
    {
        DropDownList ddlfaculties = (DropDownList)e.Row.FindControl("fact");
        string query = "select distinct facultyname from Your-Table-Name";
        SqlCommand cmd = new SqlCommand(query);
        ddlfaculties .DataSource = GetData(cmd);
        ddlfaculties .DataTextField = "facultyname";
        ddlfaculties .DataValueField = "facultyid";
        ddlfaculties .DataBind();
        ddlfaculties .Items.FindByValue((e.Row.FindControl("faculty") as Literal).Text).Selected = true;
    }
}

答案 1 :(得分:0)

将您的代码更改为以下

<强> GridView1.EditIndex

DropDownList dropdown = GridView1.Rows[GridView1.EditIndex].FindControl("fact") as DropDownList;

答案 2 :(得分:0)

使用网格视图rowdatabound evet,我们可以将数据绑定到网格视图中的Web控件

get row and find the control using edit index then bind data

答案 3 :(得分:0)

由于您尝试在编辑模式启用之前将数据绑定到下拉列表,因此出现错误。

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    gridbind();//fetch data from database and bind to grid
    DropDownList dropdown = GridView1.Rows[e.NewEditIndex].FindControl("fact") as DropDownList;
    facultyDal c = new facultyDal();
    dropdown.DataSource = c.show();
    dropdown.DataBind();
    dropdown.DataTextField = "facultyname";
    dropdown.DataValueField = "facultyid";
}