Binding DropDownList SelectedValue to DataValueField for GridView Update

22,990

This isn't really an answer to the question, but instead a workaround... but it works for me.

I added PlantMaterial_FoodID as a hidden column into the GridView and changed the SelectedValue binding on the DropDownList to reference this new column, as shown below.

New Column

<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>

...and here is the new binding

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

This effectively sets the selected dropdownlist index to the correct value.

Share:
22,990
BradV
Author by

BradV

Computer Engineer by degree, Software Dev by trade. Background in manufacturing software systems and integration projects. Now working with cloud architecture and eCommerce/ERP interfaces. I enjoy challenges and learning to leverage new tech to increase system performance and flexibility and team process efficiency. "Sucking at something is the first step towards being sorta good at something." - Jake the Dog

Updated on July 06, 2022

Comments

  • BradV
    BradV almost 2 years

    Ok guys, so I read this: How to set SelectedValue of DropDownList in GridView EditTemplate

    and I'm having the same issue. However, I don't want to bind the selected value to the displayed text, but instead the values. They are different attributes selected from a SQLDataSource. Here is my DDL code with its 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>
    

    Here is the (abridged) SQLDataSource of the GridView:

       <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>
    

    And, finally, my exception:

    DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'PlantMaterial_FoodID'.

    I really don't have much knowledge on how databinding through the GridView Edit templates work, but I was able to see that the correct values pass through the OnRowCommand event handler for updates. How do I propagate these values to the SQLDataSource without getting NULL?

    Right now, I guess any explanation on how databinding with GridView templates work in general would be beneficial as well. Thanks!

    • BradV
      BradV almost 13 years
      @naveen - added both the DataSource for the dropdown and for the GridView.
    • naveen
      naveen almost 13 years
      and the datasource / table binding the gridview? PlantMaterial_FoodID wont be present there. please paste that too
  • BradV
    BradV almost 13 years
    Yes, they are spelled the same, thanks @mika. I checked Select, Update, and Insert parameters and all GridView references and everything seems consistent as far as spelling is concerned.