'DropDownList1'有一个SelectedValue,它是无效的,因为它在项列表中不存在。参数名称:value

时间:2014-04-11 13:23:14

标签: c# asp.net gridview

我有这个代码,它提供了一个从2个数据源中检索数据的gridview。 每行显示订单详细信息,最后一列显示订单状态。 订单状态应该可以在下拉列表中更新。

<asp:GridView runat="server" AutoGenerateColumns="False" DataSourceID="OrderDataSource"
    CssClass="DataTables">
    <Columns>
        <asp:CommandField ShowEditButton="true" />
        <asp:BoundField DataField="oID" HeaderText="oId" SortExpression="oId" ReadOnly="true" />
        <asp:BoundField DataField="DateOpened" HeaderText="DateOpened" SortExpression="DateOpened"
            ReadOnly="true" />
        <asp:BoundField DataField="rName" HeaderText="rName" SortExpression="rName" ReadOnly="true" />
        <asp:BoundField DataField="DateOfArrival" HeaderText="DateOfArrival" SortExpression="DateOfArrival"
            ReadOnly="true" />
        <asp:BoundField DataField="Quantity" HeaderText="Quantity" SortExpression="Quantity"
            ReadOnly="true" />
        <asp:BoundField DataField="sName" HeaderText="sName" SortExpression="sName" ReadOnly="true" />
        <asp:TemplateField HeaderText="osName" SortExpression="osName">
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="StatusDataSource"
                    DataTextField="osName" DataValueField="osID" SelectedValue='<%# Bind("osName") %>'>
                </asp:DropDownList>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("osName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="OrderDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:igroup9_prodConnectionString %>"
    SelectCommand="spGetOrdersListForProject" SelectCommandType="StoredProcedure"
    UpdateCommand="Update[Orders] set [osID] =@osID where [oID]=@oID">
    <SelectParameters>
        <asp:ControlParameter ControlID="ProjectIDHolder" DefaultValue="" Name="ProjectID"
            PropertyName="Value" Type="Int32" />
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Name="osID" Type="Int32" />
        <asp:Parameter Name="oID" Type="Int32" />
    </UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="StatusDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:igroup9_prodConnectionString %>"
    SelectCommand="spGetOrderStatus" SelectCommandType="StoredProcedure"></asp:SqlDataSource>

问题在于,当我尝试编辑gridview中的列时,我收到此错误:(点击编辑按钮后)

Server Error in '/Maestro' Application.

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

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentOutOfRangeException: 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

1 个答案:

答案 0 :(得分:1)

错误对我来说似乎很清楚。 DropDownList中的值不包含您尝试设置为所选值的值。这可能是因为值来自osID列,所选值来自osName列。无论选择的值到底是什么,它都应来自数据源,这意味着您应该使用相同的列(或外键关系)。

您应该将其更改为...

SelectedValue='<%# Bind("osID") %>'

并确保spGetOrdersListForProject返回osID。