dropdownlist auto select first value in gridview with edit mode

1.3k Views Asked by At

actually I have bounded a dropdownlist in gridview in edit mode, but the problem is that whenever i try to select another value from dropdownlist, it auto select the first value.

I also used !IsPostBack property on page_load event but still it is not selecting the another items.

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {

            DropDownList ddlCost_Code = DropDownList)e.Row.FindControl("ddlCost_Code");

            BAL bl = new BAL();
            DataTable dt_for_CostCode = null;

            dt_for_CostCode = bl.Select_Cost_Code();
            if (dt_for_CostCode.Rows.Count > 0)
            {
                ddlCost_Code.DataSource = dt_for_CostCode;
                ddlCost_Code.DataTextField = "Cost_Code";
                ddlCost_Code.DataValueField = "Proposalno";
                ddlCost_Code.DataBind();
            }

}


  if (!IsPostBack)
    {


        try
        {

            Log_Booked_Info_Through_Booking_Table();


        }
        catch (Exception ex)
        {
            WebMsgBox.Show(ex.Message);
        }
    }

}
2

There are 2 best solutions below

0
On

Try this:

ddlCost_Code.SelectedIndex = 0;
2
On

Firstly, this will be more efficent.

if (e.Row.RowState == DataControlRowState.Edit)
{
     // your code here
}

Secondly, check the AutoPostBack property of DropDown inside your gridview. What is it set to. It should be false in your case, unless you have some logic set on the onselectedindexchanged event of dropdown.