<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    
    
Protected Sub listManufacturers_SelectedIndexChanged(ByVal sender As ObjectByVal As System.EventArgs)
        
Dim listManufacturers, listModels As DropDownList
        
Dim dsModelsByManufacturer As AccessDataSource
        
Dim currentRowInEdit As Integer = GridView1.EditIndex
        listManufacturers 
= CType(sender, DropDownList)
        listModels 
= CType(GridView1.Rows(currentRowInEdit).FindControl("listModels"), DropDownList)
        dsModelsByManufacturer 
= CType(GridView1.Rows(currentRowInEdit).FindControl("dsModelsByManufacturer"), AccessDataSource)

        dsModelsByManufacturer.SelectParameters(
"manufacturerID").DefaultValue listManufacturers.SelectedValue
        listModels.DataBind()
    
End Sub

    Protected Sub 
GridView1_RowDataBound(ByVal sender As ObjectByVal As System.Web.UI.WebControls.GridViewRowEventArgs)
        
If (e.Row.RowState And DataControlRowState.Edit) DataControlRowState.Edit Then
            Dim 
dv As System.Data.DataRowView e.Row.DataItem
            
            
' Preselect correct value in Manufacturers list
            
Dim listManufacturers As DropDownList e.Row.FindControl("listManufacturers")
            listManufacturers.SelectedValue 
dv("ManufacturerID")

            
' Databind list of models in dependent drop-down list, preselect value
            
Dim listModels As DropDownList e.Row.FindControl("listModels")
            
Dim dsc As AccessDataSource e.Row.FindControl("dsModelsByManufacturer")
            dsc.SelectParameters(
"ManufacturerID").DefaultValue dv("ManufacturerID")
            listModels.DataBind()
            listModels.SelectedValue 
dv("ModelID")
        
End If
    End Sub
    
    Protected Sub 
GridView1_RowUpdating(ByVal sender As ObjectByVal As System.Web.UI.WebControls.GridViewUpdateEventArgs)
        
Dim listManufacturers As DropDownList = CType(GridView1.Rows(e.RowIndex).FindControl("listManufacturers"), DropDownList)
        
Dim listModels As DropDownList = CType(GridView1.Rows(e.RowIndex).FindControl("listModels"), DropDownList)
        e.NewValues(
"ManufacturerID"listManufacturers.SelectedValue
        e.NewValues(
"ModelID"listModels.SelectedValue
    
End Sub

</
script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>Cascading DropDownList Controls in a GridView Control</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<br />
        <
h1>Cascading DropDownList Controls in Edit Mode of a GridView Control</h1>
        
<p>This page illustrates one way to create cascading (dependent) DropDownList controls 
           in edit mode of a GridView control. Blog post about this 
           
<a href="http://mikepope.com/blog/DisplayBlog.aspx?permalink=1708">here</a>.</p>
        
<asp:GridView ID="GridView1" 
            runat
="server" 
            AutoGenerateColumns
="False"
            DataKeyNames
="ItemID" 
            DataSourceID
="dsCarsForSale" 
            OnRowDataBound
="GridView1_RowDataBound" 
            OnRowUpdating
="GridView1_RowUpdating" 
             
>
            
            
<Columns>
                
<asp:CommandField ShowEditButton="True" />
                <
asp:BoundField 
                    DataField
="ItemID" 
                    HeaderText
="ItemID" 
                    InsertVisible
="False" 
                    ReadOnly
="True"
                    SortExpression
="ItemID" />

                <
asp:TemplateField HeaderText="Manufacturer" SortExpression="Manufacturer">
                    
<ItemTemplate>
                        
<asp:Label ID="Label1" 
                            runat
="server" 
                            Text
='<%# Bind("Manufacturer") %>'>
                        
</asp:Label>
                        
                    
</ItemTemplate>

                    
<EditItemTemplate>
                        
<asp:DropDownList 
                            ID
="listManufacturers" 
                            runat
="server" 
                            DataSourceID
="dsManufacturers" 
                            DataTextField
="Manufacturer" 
                            DataValueField
="ManufacturerID" 
                            autopostback
=true
                            OnSelectedIndexChanged
="listManufacturers_SelectedIndexChanged">
                        
</asp:DropDownList>
                         
<asp:AccessDataSource ID="dsManufacturers" 
                             runat
="server" 
                             DataFile
="~/App_Data/Cars.mdb"
                             SelectCommand
="SELECT [ManufacturerID], [Manufacturer] FROM [Manufacturers]">
                         
</asp:AccessDataSource>
                    
</EditItemTemplate>
                
</asp:TemplateField>
                
                
<asp:TemplateField HeaderText="Model" SortExpression="ModelName">
                    
<ItemTemplate>
                        
<asp:Label ID="Label2" 
                        runat
="server" 
                        Text
='<%# Bind("ModelName") %>'>
                        
</asp:Label>

                    
</ItemTemplate>

                    
<EditItemTemplate>
                       
<asp:DropDownList ID="listModels" 
                          runat
="server"
                          DataSourceID
="dsModelsByManufacturer"
                          DataTextField
="ModelName" 
                          DataValueField
="ModelID" />

                      <
asp:AccessDataSource 
                         ID
="dsModelsByManufacturer" 
                         runat
="server" 
                         DataFile
="~/App_Data/Cars.mdb"
                         SelectCommand
="SELECT [ModelID], [ModelName] FROM [Models] WHERE ([ManufacturerID] = ?)">
                         
<SelectParameters>
                             
<asp:Parameter Name="ManufacturerID" />
                         </
SelectParameters>
                      
</asp:AccessDataSource>

                    
</EditItemTemplate>
                
</asp:TemplateField>
            
</Columns>
        
</asp:GridView>
        
        
<asp:AccessDataSource ID="dsCarsForSale" 
            runat
="server" 
            DataFile
="~/App_Data/Cars.mdb"
            SelectCommand
="SELECT CarsForSale.ItemID, CarsForSale.ManufacturerID, 
                     CarsForSale.ModelID, Manufacturers.Manufacturer, Models.ModelName
                 FROM (CarsForSale 
                    INNER JOIN Manufacturers ON CarsForSale.ManufacturerID = Manufacturers.ManufacturerID) 
                 INNER JOIN Models ON CarsForSale.ModelID = Models.ModelID;"

            UpdateCommand
="UPDATE [CarsForSale] 
                SET [ManufacturerID] = ?, [ModelID] = ? 
                WHERE [ItemID] = ?"
 >
            
<UpdateParameters>
                
<asp:Parameter Name="ManufacturerID" Type="Int32" />
                <
asp:Parameter Name="ModelID" Type="Int32" />
                <
asp:Parameter Name="ItemID" Type="Int32" />
            </
UpdateParameters>
        
</asp:AccessDataSource>
        
        
</div>
    
</form>
</body>
</html>

Colorized by: CarlosAg.CodeColorizer