Reputation: 2058
I have a GridView with adrpDownList in an EditItemTemplate. The original data is in a label and in edit mode is transfered to a ddl. When pressing the edit button I recieved an exeption: System.ArgumentOutOfRangeException: 'ddlCities' has a SelectedValue which is invalid because it does not exist in the list of items. I found a similar question here and adapted the code to my needs as follows (where city is a string recieved from the label in the itemTemplate of the gridView):
protected void gvClients_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (!string.IsNullOrEmpty(city))
{
ddlTemp = (DropDownList)e.Row.Cells[7].FindControl("ddlCities");
if (ddlTemp != null)
{
ListItem item = ddlTemp.Items.FindByValue(city);
if (item != null)
{
item.Selected = true;
}
}
}
}
In order to make it work I had to erase the SelectedValue = <%# Bind("City") %> otherwise the above exception occured again. But now I want to Update my data according to the value selected in ddl and that I'm not succeeding in doing so because the ddl is not bound to anything in the gridView data source. I would appreciate help very much.
Upvotes: 0
Views: 2580
Reputation: 2058
The problem apparently is that my city data is in right to left language(Hebrew) so when the ItemTemplate Label binds to the data it adds leading spaces and therefore when binding that to the ddl SelectedValue it can't find the item in the ddl items list. That I solved by catching the RowEditing event and I extracted the text from the label using the Trim() function and put the trimmed value in a string variable called city. Then in the RowDataBound event (code in the question) selecting the proper item in the ddl succeeded. Because the ddl is not bound to data from the GridView I couldn't update the city column. For that I caught the ddl's SelectedIndexChanged event and put the selected value in a ViewState object called ViewState["CitySelect"]. Then when updating I caught the RowUpdating event as follows which enabled succesful updating of the row including the city column according to the city ddl change even though it's not bound to the gridView data source.
protected void gvClients_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = gvClients.Rows[e.RowIndex];
if (ViewState["CitySelect"] != null)
{
e.NewValues.Remove("city");
string tempCity = (string)ViewState["CitySelect"];
e.NewValues.Add("city",tempCity);
row.Cells[7].Text = (string)e.NewValues["city"];
}
else
row.Cells[7].Text = (string)e.OldValues["city"];
}
If anyone can suggest something simpler I would appreciate it.
Upvotes: 0
Reputation: 46047
Make sure that you're binding the dropdown before attempting to set it's value.
Control ddlCtrl = e.Row.FindControl("ddlCities");
if (ddlCtrl != null)
{
DropDownList ddlCities = ddlCtrl as DropDownList;
//using a datasource control
CitiesDataSourceControl.DataBind();
if (ddlCities.Items.Count > 0)
{
ListItem item = ddlCities.Items.FindByValue("Boston");
if (item != null)
item.Selected = true;
}
}
Upvotes: 1