Demo for 2-way databinding cascading lists within a GridView

CommandsCompany NameStreet AddressCountryProvinceCity
Edit WEBSWAPP Development Inc. 3070 Guildford Way Canada British Columbia Coquitlam
Edit MultiMedia CanStirr FlipFlop 277- 5 th street United States Florida Hollywood
Edit SugarPlumPrincess Kids Toys 25 Edward Ave. United Kingdom Scottland Dublin
Edit Frisian fishermen Ltd 252 Frisian Street North Netherlands North Holland Enkhuizen
Edit Seveso Bank 782 Leonardo da Vinci Ave. Italy Basilicata Potenza


This sample of the GridView is using the same concepts that were demonstrated on the FormView in the other demo within this section.

If you are still getting the message that:

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

then since you are using Bind (which will set the selected value from the data as well as save the selected value to data upon update), the dropdownlist must be populated with all possible values.

The solutions are

  1. if the problem is happening due to Null values in databound field then you can declare the dropdownlist with a null item at top while setting the AppendDataBoundItems property to true so that items are appended to (instead of replacing) the null item, like this

    <asp:DropDownList ID="ddlCountry" runat="server”

    AppendDataBoundItems=”true”

    DataSourceID="odsCountries" AutoPostBack="True"

    SelectedValue='<%# Bind("Country") %>'>

          <asp:ListItem Value="">Select an Item</asp:ListItem>

    </asp:DropDownList>

     

    For more information on the usage of the AppenDataBoundItems property refer to the MSDN: http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.appenddatabounditems(VS.80).aspx

     

  2. if the problem is happening due to illegal values saved in the databound field (that are not part of the set of values displayed on the dropdownlist) then you can:
    1. fix the data on the database
    2. create a join query within the objectdatasource for dropdownlist that would bring not only the legal values for this list but also any illegal values in the databound field
    3. handle it on the level of the user interface like I did on my sample where I removed the Bind statement from the DropDownList and replaced with code within the dropdownlist.DataBound and the GridViewRowUpdating events

GridView Code C#: Source Code in C#
GridView Code VB: Source Code in VB
SAMPLES.xsd: a dataset customized from the NorthWind database to create the demos for this website. This link has the SQL script to generate the tables that are not in NorthWind database