2011-06-09 47 views
1

玉家伙,所以我这样说的: How to set SelectedValue of DropDownList in GridView EditTemplate绑定的DropDownList的SelectedValue到DataValueField为GridView控件更新

和我有同样的问题。但是,我不想将选定的值绑定到显示的文本,而是将值绑定。它们是从SQLDataSource中选择的不同属性。这里是我的DDL代码与它的SqlDataSource:

<asp:DropDownList ID="FoodCodeDropDownList" runat="server" 
    DataSourceID="Plant_Carton_Food_List" 
    DataTextField="Food_Description" 
    DataValueField="PlantMaterial_FoodID" 
    SelectedValue='<%# Bind("PlantMaterial_FoodID") %>' > 
</asp:DropDownList> 
<asp:SqlDataSource ID="Plant_Carton_Food_List" runat="server" 
    ConnectionString="<%$ ConnectionStrings:OMSConnectionString %>" 
    SelectCommand="SELECT P.PlantMaterial_FoodID, 
       M.Material_SAP_Value + ' - ' + MD.SAP_Long_Description AS Food_Description 
      FROM Plant_Carton_Food AS P 
      LEFT OUTER JOIN Material_Descriptions AS MD 
       ON P.PlantMaterial_FoodID = MD.Material_ID 
      LEFT OUTER JOIN Materials AS M 
       ON P.PlantMaterial_FoodID = M.Material_ID"> 
</asp:SqlDataSource> 

这里是GridView控件的(有删节)的SqlDataSource:

<asp:SqlDataSource ID="Plant_Carton_Table" runat="server" 
     OldValuesParameterFormatString="old_{0}" 
     ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" 
     OnInserting="Plant_Carton_Food_Table_Inserting" 
     OnInserted="Plant_Carton_Food_Table_Inserted" 
     InsertCommand="spPlantCartonInsert" InsertCommandType="StoredProcedure" 
     SelectCommand="spPlantCartonSelect" SelectCommandType="StoredProcedure" 
     UpdateCommand="spPlantCartonUpdate" UpdateCommandType="StoredProcedure"> 
     <UpdateParameters> 
      <asp:Parameter Name="Active_Case"   Type="Boolean" /> 
      <asp:Parameter Name="PlantMaterial_FoodID" Type="String" /> 
      <asp:Parameter Name="PlantMaterial_CaseID" Type="String" /> 
      ... 
     </UpdateParameters> 
     ... 
    </asp:SqlDataSource> 

最后,我的例外:

数据绑定:' System.Data.DataRowView' 不包含名称为'PlantMaterial_FoodID'的 属性。

我真的没有太多的关于如何通过GridView编辑模板进行数据绑定的知识,但我能够看到正确的值通过OnRowCommand事件处理程序进行更新。如何将这些值传播到SQLDataSource而不会获得NULL?

现在,我想任何关于如何使用GridView模板进行数据绑定的解释都是有益的。谢谢!

+0

也粘贴您的数据源。 – naveen 2011-06-10 03:15:31

+0

@naveen - 为下拉菜单和GridView添加了DataSource。 – BradV 2011-06-10 04:26:19

+0

和datasource/table绑定gridview? PlantMaterial_FoodID不会出现在那里。请贴太 – naveen 2011-06-10 04:33:39

回答

2

这不是真正的问题的答案,而是一种解决方法......但它适用于我。

我将PlantMaterial_FoodID作为隐藏列添加到GridView中,并将DropDownList上的SelectedValue绑定更改为引用此新列,如下所示。

新列

<asp:TemplateField HeaderText="Food ID" SortExpression="Food_ID"> 
    <EditItemTemplate> 
     <asp:Label ID="FoodIDLabel" runat="server" Text='<%# Eval("Food_ID") %>'</asp:Label> 
    </EditItemTemplate> 
    <ItemTemplate> 
     <asp:Label ID="FoodIDLabel" runat="server" Text='<%# Bind("Food_ID") %>'</asp:Label> 
    </ItemTemplate> 
</asp:TemplateField> 

......这里是新的结合

<asp:DropDownList ID="FoodCodeDropDownList" runat="server" 
    DataSourceID="Plant_Carton_Food_List" DataTextField="Food_Description" 
    DataValueField="PlantMaterial_FoodID" SelectedValue='<%# Bind("Food_ID") %>' 
</asp:DropDownList> 

这有效地将所选择的下拉列表索引到正确的值。

0

您是否检查过PlantMaterial_FoodID的拼写与SqlDataSource SelectCommand中的拼写完全相同?

+0

是的,他们拼写相同,谢谢@ mika。我检查了选择,更新和插入参数以及所有的GridView引用,就拼写而言,一切看起来都是一致的。 – BradV 2011-06-09 23:39:33

相关问题