Take the 2-minute tour ×

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"
     SelectedValue='<%# Bind("PlantMaterial_FoodID") %>' >
<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">

Here is the (abridged) SQLDataSource of the GridView:

   <asp:SqlDataSource ID="Plant_Carton_Table" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" 
        InsertCommand="spPlantCartonInsert" InsertCommandType="StoredProcedure" 
        SelectCommand="spPlantCartonSelect" SelectCommandType="StoredProcedure" 
        UpdateCommand="spPlantCartonUpdate" UpdateCommandType="StoredProcedure">
            <asp:Parameter Name="Active_Case"           Type="Boolean" />
            <asp:Parameter Name="PlantMaterial_FoodID"  Type="String" />
            <asp:Parameter Name="PlantMaterial_CaseID"  Type="String" />

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!

share|improve this question
paste your datasource also. –  naveen Jun 10 '11 at 3:15
@naveen - added both the DataSource for the dropdown and for the GridView. –  BradV Jun 10 '11 at 4:26
and the datasource / table binding the gridview? PlantMaterial_FoodID wont be present there. please paste that too –  naveen Jun 10 '11 at 4:33
add comment

2 Answers

up vote 2 down vote accepted

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">
        <asp:Label ID="FoodIDLabel" runat="server" Text='<%# Eval("Food_ID") %>'</asp:Label>
        <asp:Label ID="FoodIDLabel" runat="server" Text='<%# Bind("Food_ID") %>'</asp:Label>

...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") %>'

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

share|improve this answer
add comment

Have you checked that PlantMaterial_FoodID is spelled exactly as in the SqlDataSource SelectCommand?

share|improve this answer
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. –  BradV Jun 9 '11 at 23:39
add comment

Your Answer


