0

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

4 回答 4

1

使用 RowDataBound 尝试这样的操作。

在编辑模式下绑定

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;
    }
}
于 2012-07-05T06:41:53.073 回答
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";
}
于 2013-02-18T07:35:26.153 回答
0

将您的代码更改为以下

GridView1.EditIndex

DropDownList dropdown = GridView1.Rows[GridView1.EditIndex].FindControl("fact") as DropDownList;
于 2012-05-19T11:47:08.783 回答
0

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

获取行并使用编辑索引找到控件然后绑定数据

于 2012-11-09T11:54:05.387 回答