【问题标题】:Droplist has a SelectedValue which is invalid because it does not exist in the list of itemsDroplist 有一个无效的 SelectedValue,因为它不存在于项目列表中
【发布时间】:2014-01-14 10:31:08
【问题描述】:

首先我想声明我正在使用数据表来填充我的网格视图,并且我正在使用的选择是左连接。这是我的select

SELECT PM.num [num], PMT.MedType [MedType], 
...Some other columns...
FROM [pharm_meds] PM 
LEFT JOIN [pharm_meds_Type] PMT ON  PM.[MedType_ID] = PMT.[num]

我很懒,目前有一个 asp:DropDownList 被位于 gridview 之外的 sqldatasource 填充

<asp:TemplateField ItemStyle-Width = "75px"  HeaderText = "Med Type">
        <ItemTemplate>
            <asp:Label ID="lblMedType" runat="server" Text='<%# Eval("MedType")%>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:DropDownList ID="ddlMedType" DataSourceID="sdsMedType" DataTextField="MedType" DataValueField="MedType" SelectedValue='<%# Bind("num") %>' Text='<%# Bind("num") %>' runat="server">

            </asp:DropDownList>
        </EditItemTemplate>  
        <FooterTemplate>               
            <asp:DropDownList ID="ddlMedType" DataSourceID="sdsMedType" DataTextField="MedType" DataValueField="MedType" SelectedValue='<%# Bind("num") %>' Text='<%# Bind("num") %>' runat="server" OnSelectedIndexChanged="ddlMedType_SelectedIndexChanged" AutoPostBack="True" >

            </asp:DropDownList>
        </FooterTemplate> 
        <ItemStyle Width="75px" />
    </asp:TemplateField>

这里是asp:DropDownList的sqldatasource:

<asp:SqlDataSource ID="sdsMedType" runat="server" ConnectionString="<%$ ConnectionStrings:SiteSqlServer2 %>" SelectCommand="SELECT [num], [MedType] FROM [pharm_meds_Type]"></asp:SqlDataSource>

现在我想在这里编辑现有行时遇到的麻烦是我的代码

protected void EditMedication(object sender, GridViewEditEventArgs e)
    {
        DropDownList ddlMedType = (DropDownList)gvMainView.FooterRow.FindControl("ddlMedType");
        string strMedID = ddlMedType.Text;
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SiteSqlServer2"].ConnectionString))
        {
            SqlCommand cmd1 = new SqlCommand("SELECT [num], [MedType] from [pharm_meds_Type] where [MedType] = @MedID", con);
            cmd1.Parameters.AddWithValue("@MedID", strMedID);
            con.Open();
            using (SqlDataReader reader1 = cmd1.ExecuteReader())
            {
                while (reader1.Read())
                {
                    MyMedType = reader1.GetInt32(0);
                }
                ddlMedType.SelectedValue = Convert.ToString(MyMedType); //failed attempt to fix the issue
                gvMainView.EditIndex = e.NewEditIndex;
                BindData();
            }
        }

    }

当我点击编辑链接时,标题中出现错误。我该如何解决这个问题?

【问题讨论】:

  • 开箱即用的dropdownlist 是一个可怕且不友好的控件。我建议您在填充控件之前 使用.Clear()SelectedIndex = -1SelectedValue = null(或SelectedValue = 0)。或者,您可以创建一个用户控件并改用它。
  • 您在 pharm_meds 表的“num”列中是否有不在 pharm_meds_Type 表的“num”列中的值?在我看来,这会导致您出现此错误。

标签: c# asp.net sql gridview


【解决方案1】:

出现此错误的一个原因是下拉列表写入的 SQL 表的值未包含在下拉列表值中。更常见的错误之一是数字和小数的差异。例如,0.8 和 0.800 是下拉列表中的两个不同数字。如果第一个十进制值是下拉列表选项的一部分,并且下拉列表写入的列中有 0.800,它将引发该错误。

基本上,如果您在下拉列表写入的列中有特定值并且它不属于下拉列表的一部分,则会引发此错误。

要修复它,您必须删除下拉列表,编辑网格视图,并确保数据库中的所有值都相同。要么手动编辑数据库。

【讨论】:

    猜你喜欢
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    相关资源
    最近更新 更多