locked
Do NOT refresh gridview dropdownlist on edit RRS feed

  • Question

  • User-329917403 posted

    I have a simple gridview pulling data from a SQL-server database table. It has 2 cascading dropdown lists. It’s a simple practice asp.net web application in C#. I am using a “commandfield” for the “edit” column. 
    What I am trying to do is set the dropdown lists in gridview to retain their current values when I click the edit button. As it is, when I click the edit button it refreshes the page / gridview and sets the second dropdown list back to the first selection in the menu. I want it to keep its original value unless the first dropdown list is changed. 
    This will allow me to change other boxes without affecting the two dropdown lists. 
    This simple gridview only has 5 columns: 
    Modify – (for the edit button)
    ID – (a non-editable column)
    Name – (a simple text box)
    Manufacturer – ( 1st DropDown List “ddlManufacturer”)
    Model – ( 2nd DropDown List “ddlModel”)

    What I have tried:
    I have tried setting “AutoPostBack=false” on the gridview and on both dropdown lists.
    I have also tried changing the “e.NewValue” commands on the aspx.cs page to “e.OldValue” but nothing seems to work. I have also tried putting the Gridview in an “update panel”, (didn’t work) I also tried putting the dropdownlists in an “update panel”, (that didn’t work either). Every time I click edit it resets the second dropdown list to the first value in the list instead of keeping its original value. 
    Any help would be greatly appreciated. 
    Thanks.

    Email me if you want to see the code I have so far. This is my first post here and I'm not sure how to put my code in this question or even if it's allowed. 

    Monday, September 18, 2017 4:17 PM

Answers

  • User-707554951 posted

    Hi pailwriter,

    According to your description, you need to distinct you data which is binding two dropdownlist and you  want to save the changed value in database.

    So you could refer to the code as below:

    IN ASPX:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="Id" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDataBound="GridView1_RowDataBound">
                    <Columns>
                        <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="true" />
                        <asp:TemplateField HeaderText="Name">
                            <ItemTemplate>
                                <%# Eval("Name") %>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtPrice" Text='<%# Eval("Name") %> ' runat="server"></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Manufacturer">
                            <ItemTemplate>
                                <%# Eval("Manufacturer") %>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:Label ID="lblManufacturer" runat="server" Text=' <%# Eval("Manufacturer") %>' Visible="false"></asp:Label>
                                <asp:DropDownList ID="ddlManufacturer" DataTextField="Manufacturer" DataValueField="Manufacturer" AutoPostBack="true"
                                    runat="server" Width="50px" OnSelectedIndexChanged="ddlManufacturer_SelectedIndexChanged"></asp:DropDownList>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Model">
                            <ItemTemplate>
                                <%# Eval("Model") %>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:Label ID="lblModel" runat="server" Text=' <%# Eval("Model") %>' Visible="false"></asp:Label>
                                <asp:DropDownList ID="ddlModel" DataTextField="Model" DataValueField="Model" runat="server" Width="50px"></asp:DropDownList>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:LinkButton ID="btnedit" runat="server" CommandName="Edit" Text="Edit" ></asp:LinkButton>
                                <asp:LinkButton ID="btnDelete" runat="server" CommandName="Delete" Text="Delete"></asp:LinkButton>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:LinkButton ID="btnUpdate" runat="server" CommandName="Update" Text="Update"></asp:LinkButton>
                                <asp:LinkButton ID="btncancel" runat="server" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                            </EditItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

    Code behind:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindData();
                }
            }
            private void BindData()
            {
                string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                string sqlStr = "select * from gvddl";
                DataSet ds = new DataSet();
                SqlConnection con = new SqlConnection(conStr);
                con.Open();
                SqlCommand cmd = new SqlCommand(sqlStr, con);
                SqlDataAdapter ad = new SqlDataAdapter(cmd);
                ad.Fill(ds);
                con.Close();
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
            protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
            {
                GridView1.EditIndex = e.NewEditIndex;
                BindData();
            }
            protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
            {
                GridView1.EditIndex = -1;
                BindData();
            }
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    //check if is in edit mode
                    if ((e.Row.RowState & DataControlRowState.Edit) > 0)
                    {
                        DropDownList ddlManufacturer = (DropDownList)e.Row.FindControl("ddlManufacturer");
                        DropDownList ddlModel = (DropDownList)e.Row.FindControl("ddlModel");
                        Label lblManufacturer = (Label)e.Row.FindControl("lblManufacturer");
                        
                        string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                        string sqlStr = "select distinct(Manufacturer) from [gvddl] "; // here is another table
                        DataSet ds = new DataSet();
                        SqlConnection con = new SqlConnection(conStr);
                        con.Open();
                        SqlCommand cmd = new SqlCommand(sqlStr, con);
                        SqlDataAdapter ad = new SqlDataAdapter(cmd);
                        ad.Fill(ds);
                        con.Close();
                        ddlManufacturer.DataSource = ds;
                        ddlManufacturer.DataTextField = "Manufacturer";
                        ddlManufacturer.DataValueField = "Manufacturer";
                        ddlManufacturer.DataBind();
                        ddlManufacturer.Items.FindByValue((e.Row.FindControl("lblManufacturer") as Label).Text).Selected = true;
                        string sqlStr1 = "select distinct Model from [gvddl] "; // here is another table
                        DataSet ds1 = new DataSet();
                        con.Open();
                        SqlCommand cmd1 = new SqlCommand(sqlStr1, con);
                        SqlDataAdapter ad1 = new SqlDataAdapter(cmd1);
                        ad1.Fill(ds1);
                        con.Close();
                        ddlModel.DataSource = ds1;
                        ddlModel.DataTextField = "Model";
                        ddlModel.DataValueField = "Model";
                        ddlModel.DataBind();
                        ddlModel.Items.FindByValue((e.Row.FindControl("lblModel") as Label).Text).Selected = true;
                    }
                }
            }
            protected void ddlManufacturer_SelectedIndexChanged(object sender, EventArgs e)
            {
                DropDownList ddlManufacturer = (DropDownList)sender;
                
                GridViewRow row = (GridViewRow)ddlManufacturer.NamingContainer;
                if (row != null)
                {
                    if ((row.RowState & DataControlRowState.Edit) > 0)
                    {
                        DropDownList ddlModel = (DropDownList)row.FindControl("ddlModel");
                        string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                        string sqlStr = "SELECT distinct Model FROM [gvddl] where Manufacturer='" + ddlManufacturer.SelectedValue.Trim() + "'";
                        
                        DataSet ds = new DataSet();
                        SqlConnection con = new SqlConnection(conStr);
                        con.Open();
                        SqlCommand cmd = new SqlCommand(sqlStr, con);
                        SqlDataAdapter ad = new SqlDataAdapter(cmd);
                        ad.Fill(ds);
                        
                        ddlModel.DataSource = ds;
                        ddlModel.DataTextField = "Model";
                        ddlModel.DataValueField = "Model";
                        ddlModel.DataBind();
                    }
                }
            }
            protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                string facturer = (GridView1.Rows[e.RowIndex].FindControl("ddlManufacturer") as DropDownList).SelectedItem.Value;
                string model = (GridView1.Rows[e.RowIndex].FindControl("ddlModel") as DropDownList).SelectedItem.Value;
                string Id = GridView1.DataKeys[e.RowIndex].Value.ToString();
                string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                using (SqlConnection con = new SqlConnection(strConnString))
                {
                    string query = "update gvddl set Manufacturer = @Manufacturer,Model=@Model where Id = @Id";
                    using (SqlCommand cmd = new SqlCommand(query))
                    {
                        cmd.Connection = con;
                        cmd.Parameters.AddWithValue("@Manufacturer", facturer);
                        cmd.Parameters.AddWithValue("@Model", model);
                        cmd.Parameters.AddWithValue("@Id", Id);
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                        //Response.Redirect(Request.Url.AbsoluteUri);
                    }
                }
            }

    Output:

    Related link:

    https://www.aspsnippets.com/Articles/GridView-CRUD-Select-Insert-Edit-Update-Delete-using-Single-Stored-Procedure-in-ASPNet.aspx

    Best regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 20, 2017 6:23 AM

All replies

  • User2103319870 posted

    I want it to keep its original value unless the first dropdown list is changed. 

    You need to set the Dropdownlist seleccted value on RowDatabound event. You can take a look at this article for details : http://www.c-sharpcorner.com/UploadFile/8c19e8/Asp-Net-gridview-implementing-cascading-dropdownlist-on-edit/

    Monday, September 18, 2017 4:38 PM
  • User-707554951 posted

    Hi pailwriter,

    From your description, you would retain the cuuent value in dropdownlist when you click the edit button.

    If so, I suggest that you could bind the dropdownlist select value with OnRowDataBound event.

    You could retain data like this:

    ddlManufacturer.Items.FindByValue((e.Row.FindControl("lblManufacturer") as Label).Text).Selected = true;

    You could refer to the code as below:

    In ASPX:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="Id" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowDataBound="GridView1_RowDataBound">
                    <Columns>
                        <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="true" />
                        <asp:TemplateField HeaderText="Name">
                            <ItemTemplate>
                                <%# Eval("Name") %>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtPrice" Text='<%# Eval("Name") %> ' runat="server"></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Manufacturer">
                            <ItemTemplate>
                                <asp:Label ID="lblManufacturer" runat="server" Text=' <%# Eval("Manufacturer") %>'></asp:Label>
                                <%--<%# Eval("Manufacturer") %>--%>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:Label ID="lblManufacturer" runat="server" Text=' <%# Eval("Manufacturer") %>' Visible="false"></asp:Label>
                                <asp:DropDownList ID="ddlManufacturer" DataTextField="Manufacturer" DataValueField="Manufacturer" AutoPostBack="true"
                                    runat="server" Width="50px" OnSelectedIndexChanged="ddlManufacturer_SelectedIndexChanged"></asp:DropDownList>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Model">
                            <ItemTemplate>
                                <asp:Label ID="lblModel" runat="server" Text=' <%# Eval("Model") %>'></asp:Label>
                                <%--<%# Eval("Model") %>--%>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:Label ID="lblModel" runat="server" Text=' <%# Eval("Model") %>' Visible="false"></asp:Label>
                                <asp:DropDownList ID="ddlModel" DataTextField="Model" DataValueField="Model" runat="server" Width="50px"></asp:DropDownList>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:LinkButton ID="btnedit" runat="server" CommandName="Edit" Text="Edit" ></asp:LinkButton>
                                <asp:LinkButton ID="btnDelete" runat="server" CommandName="Delete" Text="Delete"></asp:LinkButton>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:LinkButton ID="btnUpdate" runat="server" CommandName="Update" Text="Update"></asp:LinkButton>
                                <asp:LinkButton ID="btncancel" runat="server" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                            </EditItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

    In code behind:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindData();
                }
            }
            private void BindData()
            {
                string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                string sqlStr = "select * from gvddl";
                DataSet ds = new DataSet();
                SqlConnection con = new SqlConnection(conStr);
                con.Open();
                SqlCommand cmd = new SqlCommand(sqlStr, con);
                SqlDataAdapter ad = new SqlDataAdapter(cmd);
                ad.Fill(ds);
                con.Close();
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
            protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
            {
                GridView1.EditIndex = e.NewEditIndex;
                BindData();
            }
            protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
            {
                GridView1.EditIndex = -1;
                BindData();
            }
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    //check if is in edit mode
                    if ((e.Row.RowState & DataControlRowState.Edit) > 0)
                    {
                        DropDownList ddlManufacturer = (DropDownList)e.Row.FindControl("ddlManufacturer");
                        DropDownList ddlModel = (DropDownList)e.Row.FindControl("ddlModel");
                        Label lblManufacturer = (Label)e.Row.FindControl("lblManufacturer");
                        
                        string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                        string sqlStr = "SELECT *  FROM [gvddl]"; // here is another table
                        DataSet ds = new DataSet();
                        SqlConnection con = new SqlConnection(conStr);
                        con.Open();
                        SqlCommand cmd = new SqlCommand(sqlStr, con);
                        SqlDataAdapter ad = new SqlDataAdapter(cmd);
                        ad.Fill(ds);
                        ddlManufacturer.DataSource = ds;
                        ddlManufacturer.DataTextField = "Manufacturer";
                        ddlManufacturer.DataValueField = "Manufacturer";
                        ddlManufacturer.DataBind();
                        ddlManufacturer.Items.FindByValue((e.Row.FindControl("lblManufacturer") as Label).Text).Selected = true;
                        ddlModel.DataSource = ds;
                        ddlModel.DataTextField = "Model";
                        ddlModel.DataValueField = "Model";
                        ddlModel.DataBind();
                        ddlModel.Items.FindByValue((e.Row.FindControl("lblModel") as Label).Text).Selected = true;
                    }
                }
            }
            protected void ddlManufacturer_SelectedIndexChanged(object sender, EventArgs e)
            {
                DropDownList ddlManufacturer = (DropDownList)sender;
                
                GridViewRow row = (GridViewRow)ddlManufacturer.NamingContainer;
                if (row != null)
                {
                    if ((row.RowState & DataControlRowState.Edit) > 0)
                    {
                        DropDownList ddlModel = (DropDownList)row.FindControl("ddlModel");
                        string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                        string sqlStr = "SELECT * FROM [gvddl] where Manufacturer='" + ddlManufacturer.SelectedValue.Trim() + "'";
                        
                        DataSet ds = new DataSet();
                        SqlConnection con = new SqlConnection(conStr);
                        con.Open();
                        SqlCommand cmd = new SqlCommand(sqlStr, con);
                        SqlDataAdapter ad = new SqlDataAdapter(cmd);
                        ad.Fill(ds);
                        
                        ddlModel.DataSource = ds;
                        ddlModel.DataTextField = "Model";
                        ddlModel.DataValueField = "Model";
                        ddlModel.DataBind();
                    }
                }
            }

    Output:

    Best regards

    Cathy

    Tuesday, September 19, 2017 10:10 AM
  • User-329917403 posted

    Thanks Cathy:

    I tried this on my simple gridview and it worked as you stated but it does not update the gridview with the new values. 

    On mine, it also does not pull the correct data. In my sql server database named "cars" I have three tables. Customers, Manufacturer and Model. 

    With my old gridview dropdown lists (you know, the ones that would cascade but not hold their value on edit) I could get the "DISTINCT" items from the list. Using this shows the same model and Manufacturer several times in the dropdown. When I tried to put "DISTINCT" in the mix, it crapped out on me. 

    On the code behind, I changed the "SELECT * FROM gvddl" to "SELECT * FROM Customers" (since that's the name of my table)

    I have tried several options to try and get the changes saved to the gridview but nothing is actually working. 

    Any suggestions on that part? 

    Thanks so much for your help with this. I'm just a newbie trying to learn something. 

    Tuesday, September 19, 2017 5:47 PM
  • User-707554951 posted

    Hi pailwriter,

    According to your description, you need to distinct you data which is binding two dropdownlist and you  want to save the changed value in database.

    So you could refer to the code as below:

    IN ASPX:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="Id" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDataBound="GridView1_RowDataBound">
                    <Columns>
                        <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="true" />
                        <asp:TemplateField HeaderText="Name">
                            <ItemTemplate>
                                <%# Eval("Name") %>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtPrice" Text='<%# Eval("Name") %> ' runat="server"></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Manufacturer">
                            <ItemTemplate>
                                <%# Eval("Manufacturer") %>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:Label ID="lblManufacturer" runat="server" Text=' <%# Eval("Manufacturer") %>' Visible="false"></asp:Label>
                                <asp:DropDownList ID="ddlManufacturer" DataTextField="Manufacturer" DataValueField="Manufacturer" AutoPostBack="true"
                                    runat="server" Width="50px" OnSelectedIndexChanged="ddlManufacturer_SelectedIndexChanged"></asp:DropDownList>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Model">
                            <ItemTemplate>
                                <%# Eval("Model") %>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:Label ID="lblModel" runat="server" Text=' <%# Eval("Model") %>' Visible="false"></asp:Label>
                                <asp:DropDownList ID="ddlModel" DataTextField="Model" DataValueField="Model" runat="server" Width="50px"></asp:DropDownList>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:LinkButton ID="btnedit" runat="server" CommandName="Edit" Text="Edit" ></asp:LinkButton>
                                <asp:LinkButton ID="btnDelete" runat="server" CommandName="Delete" Text="Delete"></asp:LinkButton>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:LinkButton ID="btnUpdate" runat="server" CommandName="Update" Text="Update"></asp:LinkButton>
                                <asp:LinkButton ID="btncancel" runat="server" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                            </EditItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

    Code behind:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindData();
                }
            }
            private void BindData()
            {
                string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                string sqlStr = "select * from gvddl";
                DataSet ds = new DataSet();
                SqlConnection con = new SqlConnection(conStr);
                con.Open();
                SqlCommand cmd = new SqlCommand(sqlStr, con);
                SqlDataAdapter ad = new SqlDataAdapter(cmd);
                ad.Fill(ds);
                con.Close();
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
            protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
            {
                GridView1.EditIndex = e.NewEditIndex;
                BindData();
            }
            protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
            {
                GridView1.EditIndex = -1;
                BindData();
            }
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    //check if is in edit mode
                    if ((e.Row.RowState & DataControlRowState.Edit) > 0)
                    {
                        DropDownList ddlManufacturer = (DropDownList)e.Row.FindControl("ddlManufacturer");
                        DropDownList ddlModel = (DropDownList)e.Row.FindControl("ddlModel");
                        Label lblManufacturer = (Label)e.Row.FindControl("lblManufacturer");
                        
                        string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                        string sqlStr = "select distinct(Manufacturer) from [gvddl] "; // here is another table
                        DataSet ds = new DataSet();
                        SqlConnection con = new SqlConnection(conStr);
                        con.Open();
                        SqlCommand cmd = new SqlCommand(sqlStr, con);
                        SqlDataAdapter ad = new SqlDataAdapter(cmd);
                        ad.Fill(ds);
                        con.Close();
                        ddlManufacturer.DataSource = ds;
                        ddlManufacturer.DataTextField = "Manufacturer";
                        ddlManufacturer.DataValueField = "Manufacturer";
                        ddlManufacturer.DataBind();
                        ddlManufacturer.Items.FindByValue((e.Row.FindControl("lblManufacturer") as Label).Text).Selected = true;
                        string sqlStr1 = "select distinct Model from [gvddl] "; // here is another table
                        DataSet ds1 = new DataSet();
                        con.Open();
                        SqlCommand cmd1 = new SqlCommand(sqlStr1, con);
                        SqlDataAdapter ad1 = new SqlDataAdapter(cmd1);
                        ad1.Fill(ds1);
                        con.Close();
                        ddlModel.DataSource = ds1;
                        ddlModel.DataTextField = "Model";
                        ddlModel.DataValueField = "Model";
                        ddlModel.DataBind();
                        ddlModel.Items.FindByValue((e.Row.FindControl("lblModel") as Label).Text).Selected = true;
                    }
                }
            }
            protected void ddlManufacturer_SelectedIndexChanged(object sender, EventArgs e)
            {
                DropDownList ddlManufacturer = (DropDownList)sender;
                
                GridViewRow row = (GridViewRow)ddlManufacturer.NamingContainer;
                if (row != null)
                {
                    if ((row.RowState & DataControlRowState.Edit) > 0)
                    {
                        DropDownList ddlModel = (DropDownList)row.FindControl("ddlModel");
                        string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                        string sqlStr = "SELECT distinct Model FROM [gvddl] where Manufacturer='" + ddlManufacturer.SelectedValue.Trim() + "'";
                        
                        DataSet ds = new DataSet();
                        SqlConnection con = new SqlConnection(conStr);
                        con.Open();
                        SqlCommand cmd = new SqlCommand(sqlStr, con);
                        SqlDataAdapter ad = new SqlDataAdapter(cmd);
                        ad.Fill(ds);
                        
                        ddlModel.DataSource = ds;
                        ddlModel.DataTextField = "Model";
                        ddlModel.DataValueField = "Model";
                        ddlModel.DataBind();
                    }
                }
            }
            protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                string facturer = (GridView1.Rows[e.RowIndex].FindControl("ddlManufacturer") as DropDownList).SelectedItem.Value;
                string model = (GridView1.Rows[e.RowIndex].FindControl("ddlModel") as DropDownList).SelectedItem.Value;
                string Id = GridView1.DataKeys[e.RowIndex].Value.ToString();
                string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                using (SqlConnection con = new SqlConnection(strConnString))
                {
                    string query = "update gvddl set Manufacturer = @Manufacturer,Model=@Model where Id = @Id";
                    using (SqlCommand cmd = new SqlCommand(query))
                    {
                        cmd.Connection = con;
                        cmd.Parameters.AddWithValue("@Manufacturer", facturer);
                        cmd.Parameters.AddWithValue("@Model", model);
                        cmd.Parameters.AddWithValue("@Id", Id);
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                        //Response.Redirect(Request.Url.AbsoluteUri);
                    }
                }
            }

    Output:

    Related link:

    https://www.aspsnippets.com/Articles/GridView-CRUD-Select-Insert-Edit-Update-Delete-using-Single-Stored-Procedure-in-ASPNet.aspx

    Best regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 20, 2017 6:23 AM
  • User-329917403 posted

    Thank you so much Cathy. Your a genius! Sealed

    Go out and treat yourself to nice dinner. you deserve it. 

    I would buy - but alas, there's that whole "distance" thing. <grin> 

    Wednesday, September 20, 2017 9:42 PM