0


(Asp.net/C# VS2008)
我有一个由数据库填充的数据网格,当按下编辑时,会打开一个表单并从该数据网格行填充字段/控件。
我的问题在于尝试填充下拉列表框,该框具有来自 ENum 列表的数据源。
我只是无法从数据网格单元格中获取文本以显示在 ddL 中,它也应该等于枚举项之一并自动选择它。

从数据网格单元格中提取的代码给了我“低”<br> ddl_reg.Text = e.Item.Cells[25].Text;

    public void Populate_regstatus_dropdownlist()
{
    //if (!IsPostBack)
    //{
    //    ddl_reg.DataSource = Enum.GetNames(typeof(regstatus));
    //    ddl_reg.DataBind();
    //}
    //if (!IsPostBack)
    //{
    //    foreach (int value in Enum.GetValues(typeof(regstatus)))
    //    {
    //        ddl_reg.Items.Add(new ListItem(Enum.GetName(typeof(regstatus), value), value.ToString()));
    //    }
    //}
    ddl_reg.DataSource = Enum.GetNames(typeof(regstatus ));
    //ddl_reg.DataValueField = regstatus;
    //ddl_reg.DataTextField = "Low";
    //ddl_reg .SelectedItem = Enum.GetName(typeof (regstatus ));
    ddl_reg.DataBind();
    //ddl_reg.SelectedIndex = ddl_reg.Items.IndexOf(ddl_reg.Items.FindByText("Low"));


}
public enum regstatus
{
    NotSelected,
    Low,
    Medium,
    High
}

收到的错误是;

ddl_reg' 有一个无效的 SelectedValue,因为它不存在于项目列表中。参数名称:值

我是 C# 的新手,但通过搜索您的网站,我意识到这意味着看不到或未提取该值,并且真的会获得一些帮助,或者指向正确的方向,干杯。

4

1 回答 1

0

I believe the problem is that the drop down list doesn't contain the item you are passing. You may check it before changing the selected value. Other chances are that the value you are getting from e.Item.Cells[25].Text may contain spaces, so you may trim before setting it to the drop down.

 if (ddl_reg.Items.FindByText("Low") != null)
            {

                ddl_reg.Text = e.Item.Cells[25].Text;
            }
            else
            {
                //Not found
            }
于 2012-06-27T12:36:52.733 回答