Search code examples
c#asp.netgridviewdrop-down-menucascading

DropdownList SelectedValue property doesn't accept value during edit operation


I have a cascading dropdownlist inside a gridview which contains 5 columns which are ItemsType, ItemList, UnitPrice, Quantity and Total. Selecting ItemsType in 1st dropdownlist will populate items in 2nd dropdownlist i.e. ItemList which is bound to database. Problem that I am getting is that when I click Edit button, items in 2nd dropdownlist Itemstype isn't selected though I have assigned the selectedValue. When I ran the webform in debugging mode I saw that though value has been rightly assigned to dropdownlist.selectedvalue, it isn't accepting the value at all. The weird thing is that when I click the edit button for the second time after 1st debug, items in dropdownlist are selected correctly.

I have used the following

ddlCarriedItems.SelectedValue = SValue;

Though SValue has values of itemlist, SelectedValue displays none.

Here's the code.

protected void btnEdit_Click(object sender, EventArgs e)
    {
        try
        {
            Button btn = (Button)sender;
            GridViewRow gv = (GridViewRow)btn.NamingContainer;
            string VRM_id = gv.Cells[0].Text;                
            dc.Company_code = Convert.ToInt32(Session["company_code"].ToString());
            dc.Vrm_id = Convert.ToInt32(VRM_id);               
            DataTable dtVRM_CP = vrmbll.edit_VRM_carried_products(dc);
            int rowIndex = 0;
            if (dtVRM_CP.Rows.Count > 0)
            {
                for (int i = 0; i < dtVRM_CP.Rows.Count; i++)
                {                        
                    DropDownList ddlItemsType = (DropDownList)gvCarriedItems.Rows[rowIndex].Cells[1].FindControl("ddlItemsType");
                    DropDownList ddlCarriedItems = (DropDownList)gvCarriedItems.Rows[rowIndex].Cells[2].FindControl("ddlCarriedItems");
                    TextBox txtItemPrice = (TextBox)gvCarriedItems.Rows[rowIndex].Cells[3].FindControl("txtItemPrice");
                    TextBox txtItemQty = (TextBox)gvCarriedItems.Rows[rowIndex].Cells[4].FindControl("txtItemQty");
                    TextBox txtItemTotal = (TextBox)gvCarriedItems.Rows[rowIndex].Cells[5].FindControl("txtItemTotal");

                    ddlItemsType.SelectedIndex = Convert.ToInt32((dtVRM_CP.Rows[i]["items_type"]).ToString());
                    int itemstype = ddlItemsType.SelectedIndex;
                    string SValue = dtVRM_CP.Rows[i]["items"].ToString();                        
                    if (itemstype == 1)
                    {                            
                        ddlCarriedItems.SelectedValue =SValue;                               
                    }
                    else if (itemstype == 2)
                    {                            
                        ddlCarriedItems.SelectedValue = SValue;                            
                    }
                    else if (itemstype == 3)
                    {
                        ddlCarriedItems.SelectedValue = SValue;                            
                    }
                    else
                    { }
                    txtItemPrice.Text = dtVRM_CP.Rows[i]["items_price"].ToString();
                    txtItemQty.Text = dtVRM_CP.Rows[i]["items_qty"].ToString();
                    txtItemTotal.Text = dtVRM_CP.Rows[i]["items_total"].ToString();
                    if (i != (dtVRM_CP.Rows.Count) - 1 && gvCarriedItems.Rows.Count != (dtVRM_CP.Rows.Count))
                    {
                        AddNewRow();
                    }                        
                    rowIndex++;
                }
            }
        }
        catch 
        { }
    }

Solution

  • Ok I've found the problem in my code. The thing is that I had to bind dropdownlist everytime before assigning value to ddlCarriedItems.SelectedValue. I modified my code as following as per @king.code suggestion:

    if (itemstype == 1)
    {
        DataTable dt = vrmbll.select_raw_materials_production(dc);
        ddlCarriedItems.DataSource = dt;
        ddlCarriedItems.DataValueField = "ID";
        ddlCarriedItems.DataTextField = "Name";
        ddlCarriedItems.DataBind();
        ddlCarriedItems.Items.Insert(0, new ListItem("----- Select -----", "-1"));
        ddlCarriedItems.SelectedValue = SValue;
    }
    

    But I have no idea why the previous code worked when I ran debugging for 2nd time.