Asp.net 在gridview中编辑命令出错

Asp.net 在gridview中编辑命令出错,asp.net,Asp.net,我无法确定导致编辑命令失败的问题 GridView代码 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="Requestid" DataSourceID="SqlDataSource1" ForeColor="#333333" Gr

我无法确定导致编辑命令失败的问题

GridView代码

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
    AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
    DataKeyNames="Requestid" DataSourceID="SqlDataSource1" ForeColor="#333333" 
    GridLines="None" Height="321px" Width="604px">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
     <asp:CommandField ShowEditButton="True"  UpdateText="Submit" />
        <asp:BoundField DataField="Requestid" HeaderText="Requestid" ReadOnly="True" 
            SortExpression="Requestid" />
        <asp:BoundField DataField="Receiveddate" HeaderText="Receiveddate" 
            SortExpression="Receiveddate" />
        <asp:BoundField DataField="Ctname" HeaderText="Ctname" 
            SortExpression="Ctname" />
        <asp:TemplateField HeaderText="Requestor" SortExpression="Requestor">
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" 
                    DataSourceID="SqlDataSource2" DataTextField="analystname" 
                    DataValueField="contact_id" SelectedValue='<%# Bind("Requestor") %>'>
                </asp:DropDownList>
                <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:Customers %>"         SelectCommand="SELECT [contact_id], [first_name]+ space(1)+ [last_name] as analystname FROM [contact]
order by contact_id"></asp:SqlDataSource>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Requestor") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <EditRowStyle BackColor="#CC3300" />
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#CBB06D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#F5F7FB" />
    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
    <SortedDescendingCellStyle BackColor="#E9EBEF" />
    <SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:Customers %>" SelectCommand="SELECT RR.Requestid,Convert(varchar(20),RR.Receiveddate,100) as Receiveddate,RR.Ctname,C.First_Name +Space(1) + C.Last_NAME as Requestor  from report_request_draft RR inner join Contact C on RR.requestor = C.contact_id
where clonedfromid is not null"

UpdateCommand="UPDATE [REPORT_REQUEST_DRAFT] SET  [REQUESTOR] = @Requestor  WHERE [requestid] = @requestid">

</asp:SqlDataSource>

当我点击编辑命令按钮时,它给出了以下错误

错误

“DropDownList 1”有一个SelectedValue,该值无效,因为它不在项目列表中。 参数名称:value 描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源

异常详细信息:System.ArgumentOutOfRangeException:“DropDownList1”有一个SelectedValue,该值无效,因为它不在项目列表中。
参数名称:value

不能以这种方式绑定DropDownList。通过使用现有的
SelectedValue
绑定,它要求在绑定GridView之前绑定DropDownList,但这在逻辑上是不可能的,因为只有在GridView完成绑定后才能绑定DropDownList。您应该在
RowDataBound
事件中将此逻辑放在代码后面。此时,您应该手动绑定DropDownList,然后设置其
SelectedValue
属性。

请考虑以下链接,它将帮助您解决问题,并向您展示如何在网格视图中完美绑定EditItemTemplet。我希望您使用链接更好地理解网格视图。

Dropdownlist在edititem模板中。因此,只有当我单击edit命令按钮时,下拉列表才会绑定。我以前能够实现此功能,但现在出现此错误