I have a Sharepoint list containing a few fields that includes 2 lookup columns(department and manager). In edit mode, my lookup columns appear to be dropdown lists which say "select".

It should contain the value that was already stored in the SP list! How do I do that? Also help me with updating values to the SP list. I face issues since I have 2 Lookups and also I'm a newbie.

Some help would be appreciated! Thanks :)

The following is the code for 2 dropdown lists (department and manager)

            <asp:TemplateField HeaderText="Department">
                    <%--<asp:Label ID="TextBoxEditDepartment" runat="server" Text='<%# Eval("TextBoxEditDepartment") %>' />--%>
                    <asp:DropDownList ID="ddlDepartment" runat="server">
                        <asp:ListItem Value="0">Select</asp:ListItem>
                        <asp:ListItem Value="1">Applied Materials</asp:ListItem>
                        <asp:ListItem Value="2">Airbus</asp:ListItem>
                        <asp:ListItem Value="3">Nestle</asp:ListItem>
                        <asp:ListItem Value="4">GE</asp:ListItem>
                        <asp:ListItem Value="5">Bombardier Aerospace</asp:ListItem>
                    <asp:Label ID="LabelDepartment" runat="server" Text='<%# Eval("LabelDepartment") %>' />
                    <asp:TextBox ID="TextBoxDepartment" runat="server" />

            <asp:TemplateField HeaderText="Manager">
                    <%--<asp:Label ID="TextBoxEditManager" runat="server" Text='<%# Eval("TextBoxEditManager") %>' />--%>
                    <asp:DropDownList ID="ddlManager" runat="server">
                        <asp:ListItem Value="0">Select</asp:ListItem>
                        <asp:ListItem Value="1">Sunil Narayanappa</asp:ListItem>
                        <asp:ListItem Value="2">Rohini Singh</asp:ListItem>
                        <asp:ListItem Value="3">Ananth Rao</asp:ListItem>
                        <asp:ListItem Value="4">Vinyas Gowda</asp:ListItem>
                        <asp:ListItem Value="5">Shilpa Shreedhar</asp:ListItem>
                    <asp:Label ID="LabelManager" runat="server" Text='<%# Eval("LabelManager") %>' />
                    <asp:TextBox ID="TextBoxManager" runat="server" />

The below code is for update method

public void updateRow(string itemID, string firstName, string lastName, string age, string eAddress, string department, string manager, string gender, string salary)
        ClientContext clientContext = new ClientContext("https://xyz.xyz.com/sites/xyz/TrainingSite/");

            SP.List oList = clientContext.Web.Lists.GetByTitle("Employees");
            //Microsoft.SharePoint.Client.ListItem oListItem = oList.GetItemById(Emp.iddd);
            SP.CamlQuery camlQuery = new SP.CamlQuery();
            camlQuery.ViewXml = @"<Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + itemID + "</Value></Eq></Where></Query>";
            SP.ListItemCollection itemInfo = oList.GetItems(camlQuery);
            foreach (SP.ListItem item in itemInfo)
                if (itemID == item["ID"].ToString())
                    item["Title"] = firstName;
                    item["Last_x0020_Name"] = lastName;
                    item["u5ib"] = age;
                    item["Address"] = eAddress;

                    //Department column
                    //item["Department"] = department;
                    FieldLookupValue deptItem = new FieldLookupValue();
                    //deptItem.LookupId = Convert.ToInt32(TextBoxEditDepartment.SelectedValue); // here instead of 1, you should provide corresponding Department list item ID
                    item["Department"] = deptItem;
                    deptItem.LookupId = Convert.ToInt32(department);

                    //Manager column
                    //item["Manager"] = manager;
                    FieldLookupValue mgrItem = new FieldLookupValue();
                    item["Manager"] = mgrItem;
                    mgrItem.LookupId = Convert.ToInt32(manager);

                    item["Gender"] = gender;
                    item["Salary"] = salary;
        catch (Exception e)
            throw e;

The following is the RowUpdating event

protected void gridview_RowUpdating(object sender, GridViewUpdateEventArgs e)
        //Accessing Edited values from the GridView
        TextBox itemID = (TextBox)gridview.Rows[e.RowIndex].Cells[8].FindControl("VJ"); //ID
        TextBox firstName = (TextBox)gridview.Rows[e.RowIndex].Cells[0].FindControl("TextBoxEditFirstName"); //firstName
        TextBox lastName = (TextBox)gridview.Rows[e.RowIndex].Cells[1].FindControl("TextBoxEditLastName"); //lastName
        TextBox age = (TextBox)gridview.Rows[e.RowIndex].Cells[2].FindControl("TextBoxEditAge"); //age
        TextBox eAddress = (TextBox)gridview.Rows[e.RowIndex].Cells[3].FindControl("TextBoxEditAddress"); //eAddress

        //string department = ((DropDownList)gridview.Rows[e.RowIndex].Cells[4].FindControl("ddlDepartment")).SelectedValue;

        DropDownList department = (DropDownList)gridview.Rows[e.RowIndex].Cells[4].FindControl("ddlDepartment"); //department
        DropDownList manager = (DropDownList)gridview.Rows[e.RowIndex].Cells[5].FindControl("ddlManager"); //manager
        TextBox gender = (TextBox)gridview.Rows[e.RowIndex].Cells[6].FindControl("TextBoxEditGender"); //gender
        TextBox salary = (TextBox)gridview.Rows[e.RowIndex].Cells[7].FindControl("TextBoxEditSalary"); //salary

        updateRow(itemID.Text, firstName.Text, lastName.Text, age.Text, eAddress.Text, department.Text, manager.Text, gender.Text, salary.Text); // call update method

        gridview.EditIndex = -1; //Turn the Grid to read only mode
        //this.gridview_DataBind(department, e.RowIndex);
        BindGrid(); // Rebind GridView to reflect changes made

        Response.Write("Update Seccessful!");

    private void gridview_DataBind(string department, int index)
        DataTable dt = (DataTable)Session["variableTable"]; //features a DataTable with the Contents of the Gridview
        dt.Rows[index]["ControlType"] = department;
        gridview.DataSource = dt;

1 Answer 1


From your question i see that you want to set a selected value in drop-down when grid-view is edited. To get that done 1. Get the drop-down control, get the items in options and set the value by passing selected index OR selected value

The entire piece of code can be like this

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    if (e.Row.RowType == DataControlRowType.DataRow)
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            DropDownList ddList= (DropDownList)e.Row.FindControl("drpcategory1");
            //bind dropdown-list
            DataTable dt = ListToDataTable(ListItemCollectionObject);
            ddList.DataSource = dt;
            ddList.DataTextField = "myField_name";
            ddList.DataValueField = "myitemId";

            DataRowView dr = e.Row.DataItem as DataRowView;
            //ddList.SelectedItem.Text = dr["category_name"].ToString();
            ddList.SelectedValue = dr["category_name"].ToString();

Update To get items so that you can bind gridview from list use below logic, you will have to create datatable.

private DataTable ListToDataTable(List myList, Microsoft.SharePoint.Client.ListItemCollection items)
    DataTable dt = new DataTable();


    DataRow row;

    foreach(Microsoft.SharePoint.Client.ListItem item in items)
        row = dt.Rows.Add();
        row["From"] = item["From1"].ToString();
        row["To"] = item["To"].ToString();
        row["Subject"] = item["Subject1"].ToString();
        row["Body"] = item["Body1"].ToString();
        row["Attachments"] = item["Attachments"].ToString();
        row["Sent"] = item["Sent"].ToString();
    return dt;
  • I am not using any DB, So how can this be done ? @Akshay
    – Sanjana V
    Aug 14, 2017 at 6:28
  • @SanjanaV- updated answer for your doubt Aug 14, 2017 at 6:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.