Hi , i hv created 2 tables : a Department table with fields Deptname , Deptid and Employee table with Empid , Empname, Salary .
I have created a datagrid with fields empname , address , Salary , Deptname and edit and delete command columns . By clicking on edit , it redirects to another form with 3 textboxes ( name , address , salary) and a dropdownlist with department names and update button .So data will get edited and updated after clicking on update button .
I have binded the dropdownlist with values from database . I have used a query string using which data from datagrid is populated into the textboxes on the second page .
The problem that I am facing is that the deptnames for each row in datagrid r not getting selected in the dropdownlist while trying to edit.
The code used for binding the dropdownlist with database is as follows :

    protected void FillDeptDropDownList()
    {
        String strConnString = ConfigurationManager.ConnectionStrings["Company"].ConnectionString;
        SqlConnection con = new SqlConnection(strConnString);
        SqlCommand cmd = new SqlCommand("Select * from Department", con);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        adp.Fill(dt);
        DropDownList1.DataSource = dt;
        DropDownList1.DataTextField = "DeptName";
        DropDownList1.DataValueField = "DeptID";
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, "Select department name:");



    }

Please help with dis issue .

The above code for filling the dropdownlist is correct. Are you binding the dropdownlist in the OnRowDataBound of Gridview? In OnRowDataBound before

if (e.Row.RowType == DataControlRowType.DataRow)

        //Find the DropDownList in the Row
        DropDownList ddlCountries = (e.Row.FindControl("ddldepartment") as DropDownList);
        //continued by your code

    }

Check this out for further details.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.