This is a migrated thread and some comments may be shown as answers.

Help with custom drop down when editing

2 Answers 61 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Greg Barretta
Top achievements
Rank 1
Greg Barretta asked on 23 Mar 2010, 11:40 PM
I am defining a Grid Template Column using a label when in display and using a dropdown during edit and "add new". I am doing this exactly as given in example here. There is an important flaw in this example. When editiing, the example selects the Country in the drop down that matches the record that was previously saved, not the Country of the item being edited. Can anyone help me using this example to determine how to pull the Country of the item you are editing and in turn select the appropriate drop down.

2 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 24 Mar 2010, 07:39 AM

Hi Greg,

You can fix this by setting the list.SelectedValue from DataRowView value rather than setting from Session["updatedValue"], inside ItemDataBound event.

Try the following code inside ItemDataBound and see whether it helps:

C#:

if (e.Item is GridEditableItem && e.Item.IsInEditMode) 
    GridEditableItem item = e.Item as GridEditableItem; 
    // access/modify the edit item template settings here 
    DropDownList list = item.FindControl("List1"as DropDownList; 
    list.DataSource = Country_values; 
    list.DataBind(); 
    DataRowView row = (DataRowView) item.DataItem;                
    list.SelectedValue = row["Country"].ToString(); 
 
 

-Shinu
0
Greg Barretta
Top achievements
Rank 1
answered on 24 Mar 2010, 04:17 PM
Thanks Shinu, you put me in the right direction. My datasource is a LINQ query so I just needed to parse as an anonymous type.

list.SelectedValue =

DataBinder.Eval(item.DataItem, "country").ToString(); ;

 

Tags
Grid
Asked by
Greg Barretta
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Greg Barretta
Top achievements
Rank 1
Share this question
or