Click here to Skip to main content
15,790,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have dropdown list inside item template of Gridview.
Dropdown list items are added dynamically at Girdview RowDataBound.
I used the command field Edit to update the selected value of dropdown list into database.Update process is fine. My problem is when user select the
value and click update, the selected value inside the dropdown list is lost and changed to first item of the list. I want to set the selected value of the dropdown list unless user update. Please somebody help me in how to set the selected value of dropdown list.

Thank you in advance. Looking for the help. :)

Maybe you need to rebind the grid in the edit postback--after you've updated the dataset with the change?
Share this answer
private void SetPreviousData()
        int rowindex = 0;
        if (ViewState["CurrentTable"] != null)
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
                for (int i = 0; i < dt.Rows.Count; i++)
                    DropDownList ddl = (DropDownList)grdRoomDetails.Rows[rowindex].Cells[1].FindControl("ddlRoomCategory");
                    DropDownList ddlRoom = (DropDownList)grdRoomDetails.Rows[rowindex].Cells[2].FindControl("ddlRoom");
                    //Againg binding the previous rows grid COntrols and Retaining their values
                    CategoryBLL obj = new CategoryBLL();
                    DataTable dt1 = new DataTable(); 
                    dt1 = obj.Show_Category();
                    ddl.DataSource = dt1;
                    ddl.DataTextField = "CategoryName";
                    ddl.DataValueField = "CategoryID";
                    ddl.Items.Insert(0, "--Select--");
                    ddl.Items[0].Value = "0";
                    ddl.SelectedValue = dt.Rows[rowindex]["RoomCategory"].ToString();

                    ReservationBLL objres = new ReservationBLL();
                    objres.CategoryID = Convert.ToInt32(ddl.SelectedValue);
                    ddlRoom.DataSource = objres.Show_Room();
                    ddlRoom.DataValueField = "RoomID";
                    ddlRoom.DataTextField = "RoomNo";
                    ddlRoom.Items.Insert(0, "--Select--");
                    ddlRoom.Items[0].Value = "0";
                    ddlRoom.SelectedValue = dt.Rows[rowindex]["Room"].ToString();

Share this answer
André Kraak 30-Mar-12 16:05pm    
Edited solution:
Added pre tags
In your Gridview RowdataBound event, after you have added the items dynamically, find the selected value for that dropdown and set the selectedindex to that value.

[EDIT: Provided order of things to be executed]

In Gridview RowdataBound event pseudo code:

'code to extract the selected value from the database/dataset/wherever
selectedvalue = getSelectedValue(Primary key) ' change as appropriate

'your code to fill the completed drop down
do while eof
  add the item to dropdown
  if item.value = selectedvalue then  
      item.selected=true ' set selection
  end if
Share this answer
Teddy Su 29-Mar-12 0:07am    
Dim li As ListItem = CType(ddlist.SelectedItem, ListItem)
ddllist.SelectedIndex = list.Items.IndexOf(li)

I tried the above code. But the Selected Item of dropdown list is showing the first item of the dropdown list. It doesn't know the user selected value.
I dont know how to get the user selected value form row databound. At SelectedIndex Changed event of dropdown , I can get the user selected value .
Thanks for your solution.
shreekar 30-Mar-12 15:07pm    
I have updated the answer to show the changes

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900