0

我有一个DropDownlistGridView只有在单击编辑时才应该可见。我已经绑定了DropDownList后面的代码。当我单击编辑时,该单元格的标签值应自动在DropDownList.

我试过的代码是:

protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            SqlCommand cmd = new SqlCommand("SELECT Location_Name FROM Location_Table");
            DropDownList bind_drop = (e.Row.FindControl("DropList") as DropDownList);
            bind_drop.DataSource = this.ExecuteQuery(cmd, "SELECT");
            bind_drop.DataTextField = "Location_Name";
            bind_drop.DataValueField = "Location_Name";
            bind_drop.DataBind();

            string Loc_type = (e.Row.FindControl("id2") as Label).Text.Trim();               
            bind_drop.Items.FindByValue(Loc_type).Selected = true;
        }
    }

当我运行代码时,它给出了一个异常错误对象引用未在上述代码的最后一行设置。查不出什么问题。请帮助

4

1 回答 1

1

您必须确保您的列表包含标签值。

var index = DropDownList1.Items.IndexOf(Loc_type );
if(index > 0)
{
  DropDownList1.SelectedIndex = index;
}
else
{
  Console.WriteLine("item does not exist");
}
于 2012-10-12T15:36:03.980 回答