2012-01-12 89 views
1

嗯,我有帶有DropDownList的'TemplateField'等可編輯字段的GridView。 我的代碼:GridView:在GridView中編輯底層數據

<Columns> 
    ... 
    <asp:TemplateField SortExpression="Room" HeaderText="Room"> 
     <EditItemTemplate> 
      <asp:DropDownList ID="DropDownList1" Runat="server" DataSourceID="categoryDataSource" DataTextField="RoomNumber" DataValueField="RoomNumber" SelectedValue='<%# Bind("Room") %>'> 
      </asp:DropDownList> 
     </EditItemTemplate> 
    </asp:TemplateField> 
    <ItemTemplate> 
     <asp:Label Runat="server" Text='<%# Bind("Room") %>' ID="Label1"></asp:Label> 
    </ItemTemplate> 
</Columns> 

我DB具有表房間,裏面有行:RoomId,RoomNumber。 在我的DropDownList中,我嘗試設置表格房間的所有值。 表有3行(RoomId,RoomNumber):1 - 20; 2 - 12; 3 - 24

但寫遵循錯誤:

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

參數名:價值 也許有人知道我在哪裏犯了錯誤?

編輯:這裏是SqlDataSource的代碼

<asp:SqlDataSource ID="categoryDataSource" Runat="server" 
    SelectCommand="SELECT [RoomId], [RoomNumber] FROM [Rooms] ORDER BY [RoomNumber]" 
    ConnectionString="Data Source=.\MSSQLSERVERR2;AttachDbFilename=|DataDirectory|\ARMDB.MDF;Integrated Security=True;User Instance=True"> 
</asp:SqlDataSource> 

回答

3

的錯誤表明您正在嘗試爲入選下拉列表中設置值不存在。我相信這種情況的發生主要是因爲在下拉列表中沒有爲價值選擇錯誤的字段或設置錯誤的值。

例如,在你的情況下,最有可能,你在引用表中存儲RoomId(而不是RoomNumber),所以你需要的值綁定字段的下拉列表中相應

<asp:DropDownList ID="DDL1" Runat="server" DataSourceID="categoryDataSource" 
    DataTextField="RoomNumber" DataValueField="RoomId" 
    SelectedValue='<%# Bind("Room") %>'> 

DataValueField值 - 它的RoomId而不是房間號。 (假設Bind("Room")是否會返回房間ID)

+0

代碼是在這裏(谷歌文檔):https://docs.google.com/document/d/1R7FU5Ka85G2PAbJJ5vj3MM1Tcd4rDNI-ETOw8oSrnxw/edit – Leo 2012-01-12 22:58:20

+0

它不工作...我不明白哪裏有問題... – Leo 2012-01-12 22:59:45