Click here to Skip to main content
12,252,903 members (58,265 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB VB.NET
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. Smile | :)
Posted 28-Mar-12 0:03am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Maybe you need to rebind the grid in the edit postback--after you've updated the dataset with the change?
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.DataBind();
                    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.DataBind();
                    ddlRoom.Items.Insert(0, "--Select--");
                    ddlRoom.Items[0].Value = "0";
                    ddlRoom.SelectedValue = dt.Rows[rowindex]["Room"].ToString();
 
                    rowindex++;
                }
            }
  Permalink  
v2
Comments
André Kraak 30-Mar-12 16:05pm
   
Edited solution:
Added pre tags
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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
loop
  Permalink  
v2
Comments
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 30 Mar 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100