2012-12-05 70 views
0

蔭不能確定這是造成編輯命令失敗編輯命令gridiview示數出

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> 

當我在編輯命令按鈕,點擊它給了我下面的錯誤問題

錯誤

'DropDownList1' 具有的SelectedValue這是無效的,因爲它ð項目列表中不存在。 參數名稱:值 描述:執行當前Web請求期間發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。

異常詳細信息:System.ArgumentOutOfRangeException:'DropDownList1'具有一個無效的SelectedValue,因爲它在項目列表中不存在。 參數名稱:值

回答

0

DropDownList不能以這種方式進行綁定。通過使用SelectedValue綁定,您需要在綁定GridView之前綁定DropDownList,但這是不可能的,因爲在GridView完成綁定之前,DropDownList無法綁定。您應該在RowDataBound事件中將此邏輯放在代碼後面。在這一點上,你應該手動綁定你的DropDownList,然後設置它的SelectedValue屬性。

+0

Dropdownlist是在edititem template.so只有當我點擊編輯命令按鈕,然後下拉列表binded.I能夠實現此功能之前,但現在得到此錯誤 –

0

請考慮以下鏈接,它將幫助您解決問題,並且還向您展示如何在網格視圖中完美地綁定EditItemTemplet。我爲了更好地理解網格視圖而使用this鏈接來聚集你。