简体   繁体   中英

Trying to populate drop-down list in GridView edit

I have the following code trying to populate drop-down list when I click edit on a Grid View, and it gives me the following error:

" 'ddlgvRoom' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value"

Any idea on why I need to add code into the row editing event, and if so can you help? My gridview is getting its values from an objectdatasource.

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow &&
            (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
        {
            DropDownList dl = (DropDownList)e.Row.FindControl("ddlgvRoom");
        }
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        DropDownList ddlgvRoom = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlgvRoom");
        string strgvRoom = ddlgvRoom.SelectedItem.Text.ToString();

        DropDownList ddlgvJack = (DropDownList)
            GridView1.Rows[e.RowIndex].FindControl("ddlgvJack");
        string strgvJack = ddlgvJack.SelectedItem.Text.ToString();

        DropDownList ddlgvVlan = (DropDownList)
            GridView1.Rows[e.RowIndex].FindControl("ddlgvVlan");
        string strgvVlan = ddlgvVlan.SelectedItem.Text.ToString();
        GridView1.DataBind();

    }
}

必须脱掉HTML端的selectedvalue,一切都开始起作用

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM