c# - Droplist 有一个无效的 SelectedValue,因为它不存在于项目列表中

标签 c# asp.net sql gridview

首先,我想声明我正在使用数据表来填充我的 GridView ,并且我正在使用的选择是左连接。这是我的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();
            }
        }

    }

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

最佳答案

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

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

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

关于c# - Droplist 有一个无效的 SelectedValue,因为它不存在于项目列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20790404/

相关文章:

mysql - 有计数不起作用

c# - SQL查询的转换结果-System.InvalidCastException

c# - 仅返回字段时使用 AsNoTracking() 会有所不同吗?

c# - 显示无/删除样式背后的asp.net 代码不起作用

mysql - 如何在mysql中只返回唯一值

asp.net - Crystal Reports 仅在本地显示条形码

c# - 如何在 Asp.net Web 服务器端启动 bat 文件?

c# - 如何将 TCPListener 设置为始终监听以及新连接何时丢弃当前连接

c# - 从 C# 应用程序编辑 Word .docx 文件

c# - Global.asax 在小改动后解析错误并恢复到以前的版本