UPDATE
I have figured out a solution to my problem, which I am sure there are others who would like to know the answer. So, here it is,
My code was basically on track, I was only missing a couple of items.
First; I copied the "asp:Label…. " command from the "ItemTemplate" section of the "Model" column and pasted it in the "EditItemTemplate" just above the dropdown list and added the value (Visible="false") to the end of the statement.
Second; I added the following (GridView1_RowDataBound) method to the "aspx.cs" page just above the existing (GridView1_RowUpdating) method and populated it with the following code –
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList DDL2 = e.Row.FindControl("ddlModel") as DropDownList;
Label LBL2 = e.Row.FindControl("Label1") as Label;
DDL2.DataBind();
DDL2.SelectedValue = Convert.ToString(LBL2.Text);
}
}
Finally, I added the (OnRowDataBound=”GridView1_RowDataBound”) command to the head of the Gridview where you actually call the Gridview.
Here is the full source code for both pages:
HTML page (default.aspx)
<%@ Page Title="Cascading Gridview" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="About.aspx.cs" Inherits="WebApplication1.About" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Customerid" DataSourceID="SqlDataSource1"
CellPadding="4" ForeColor="#333333" OnRowUpdating="GridView1_RowUpdating" OnRowDataBound="GridView1_RowDataBound">
<AlternatingRowStyle BackColor="White" />
<Columns>
<%----%>
<asp:CommandField ShowEditButton="true" />
<%----%>
<asp:BoundField DataField="Customerid" HeaderText="Customerid" InsertVisible="False" ReadOnly="True" SortExpression="Customerid" />
<%----%>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<%----%>
<asp:TemplateField HeaderText="Manufacturer" SortExpression="Manufacturer">
<EditItemTemplate>
<%----%>
<%----%>
<asp:DropDownList ID="ddlManufacturer" runat="server" AutoPostBack="true" DataTextField="Manufacturer"
DataValueField="Manufacturer" SelectedValue='<%# Bind("Manufacturer") %>' DataSourceID="SqlDataSource2" >
</asp:DropDownList>
<%----%>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:conString %>"
SelectCommand="SELECT [Manufacturer] FROM Manufacturer"></asp:SqlDataSource>
</EditItemTemplate>
<%----%>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Manufacturer") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<%----%>
<asp:TemplateField HeaderText="Model" SortExpression="Model">
<EditItemTemplate>
<%----%>
<%----%>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Model") %>' Visible="false"></asp:Label>
<%----%>
<asp:DropDownList ID="ddlModel" runat="server" DataSourceID="SqlDataSource3" DataTextField="Model" DataValueField="Model" >
</asp:DropDownList>
<%----%>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:conString %>"
SelectCommand="SELECT [Model],[Manufacturer] FROM [Model] " FilterExpression="[Manufacturer]= '{0}'">
<FilterParameters><asp:ControlParameter ControlID="ddlManufacturer" Name="Manufacturer" PropertyName="SelectedValue" Type="String" />
</FilterParameters>
</asp:SqlDataSource>
</EditItemTemplate>
<%----%>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Model") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<%----%>
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id" />
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:conString %>"
SelectCommand="SELECT [CustomerId], [Name], [Manufacturer], [Model], [id] FROM [Customers] ORDER BY CustomerId"
UpdateCommand="UPDATE [Customers] SET [Name]=@Name, [Manufacturer]=@Manufacturer, [Model]=@Model WHERE [CustomerId]=@CustomerId"></asp:SqlDataSource>
</asp:Content>
Code behind page (default.aspx.cs)
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class About : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList DDL2 = e.Row.FindControl("ddlModel") as DropDownList;
Label LBL2 = e.Row.FindControl("Label1") as Label;
DDL2.DataBind();
DDL2.SelectedValue = Convert.ToString(LBL2.Text);
}
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string strModel = ((DropDownList)((GridView)sender).Rows[e.RowIndex].FindControl("ddlModel")).SelectedValue;
string strManufacturer = ((DropDownList)((GridView)sender).Rows[e.RowIndex].FindControl("ddlManufacturer")).SelectedItem.Value;
e.NewValues[
"Model"] = strModel;
e.NewValues[
"Manufacturer"] = strManufacturer;
e.Cancel =
false;
GridView gv = (GridView)sender;
for (int i = 0; i < gv.Columns.Count; i++)
{
DataControlFieldCell cell = gv.Rows[e.RowIndex].Cells[i] as DataControlFieldCell;
gv.Columns[i].ExtractValuesFromCell(e.NewValues, cell,
DataControlRowState.Edit, true);
}
}
}
}