Setting of DataGrid's SelectedIndex appears to be ignored
-
I'm having a bit of trouble with the code below. The idea was to check the relative position of the DataGrid's selected DataKeyValue in the Typed Data Set's DataTable AFTER the DataTable's Fill() and BEFORE the DataGrid's DataBind(). If rows had been deleted or inserted I would set the SelectedIndex to the new position, based on the key rather than the previous SelectedIndex. When the new page was rendered, the correct item would be Selected. I've traced through the code and verified that immediately prior to the DataBind() the SelectedIndex is set to the correct (new) value but when it's rendered in the browser, the original position is displayed using the SelectedItemStyle. I'm sure of the loop and the value of
dgAttributeCategory.SelectedIndex
prior to the call todgAttributeCategory.DataBind();
Would someone please humiliate me and tell me I'm doing something really dumb?//Check for movement of the Selected Item within the Grid and correct if needed. int i = 0; bool boolItHasBeenDeleted = true; foreach (dsAttributeTypes.AttributeCategoryRow row in dsAttributeTypes1.AttributeCategory) { if (row.AttributeCategoryId == guidAttributeCategoryId_Before) { if (dgAttributeCategory.SelectedIndex != -1) **dgAttributeCategory.SelectedIndex = i;** else dgAttributeCategory.EditItemIndex = i; boolItHasBeenDeleted = false; break; } else { i++; } } if (boolItHasBeenDeleted){//Warn the user} dgAttributeCategory.DataBind();
Thanks, Will -
I'm having a bit of trouble with the code below. The idea was to check the relative position of the DataGrid's selected DataKeyValue in the Typed Data Set's DataTable AFTER the DataTable's Fill() and BEFORE the DataGrid's DataBind(). If rows had been deleted or inserted I would set the SelectedIndex to the new position, based on the key rather than the previous SelectedIndex. When the new page was rendered, the correct item would be Selected. I've traced through the code and verified that immediately prior to the DataBind() the SelectedIndex is set to the correct (new) value but when it's rendered in the browser, the original position is displayed using the SelectedItemStyle. I'm sure of the loop and the value of
dgAttributeCategory.SelectedIndex
prior to the call todgAttributeCategory.DataBind();
Would someone please humiliate me and tell me I'm doing something really dumb?//Check for movement of the Selected Item within the Grid and correct if needed. int i = 0; bool boolItHasBeenDeleted = true; foreach (dsAttributeTypes.AttributeCategoryRow row in dsAttributeTypes1.AttributeCategory) { if (row.AttributeCategoryId == guidAttributeCategoryId_Before) { if (dgAttributeCategory.SelectedIndex != -1) **dgAttributeCategory.SelectedIndex = i;** else dgAttributeCategory.EditItemIndex = i; boolItHasBeenDeleted = false; break; } else { i++; } } if (boolItHasBeenDeleted){//Warn the user} dgAttributeCategory.DataBind();
Thanks, WillDataBind resets the datagrid, including selected items. If you want to select an item, you should do it after data binding. Christian Graus - Microsoft MVP - C++
-
I'm having a bit of trouble with the code below. The idea was to check the relative position of the DataGrid's selected DataKeyValue in the Typed Data Set's DataTable AFTER the DataTable's Fill() and BEFORE the DataGrid's DataBind(). If rows had been deleted or inserted I would set the SelectedIndex to the new position, based on the key rather than the previous SelectedIndex. When the new page was rendered, the correct item would be Selected. I've traced through the code and verified that immediately prior to the DataBind() the SelectedIndex is set to the correct (new) value but when it's rendered in the browser, the original position is displayed using the SelectedItemStyle. I'm sure of the loop and the value of
dgAttributeCategory.SelectedIndex
prior to the call todgAttributeCategory.DataBind();
Would someone please humiliate me and tell me I'm doing something really dumb?//Check for movement of the Selected Item within the Grid and correct if needed. int i = 0; bool boolItHasBeenDeleted = true; foreach (dsAttributeTypes.AttributeCategoryRow row in dsAttributeTypes1.AttributeCategory) { if (row.AttributeCategoryId == guidAttributeCategoryId_Before) { if (dgAttributeCategory.SelectedIndex != -1) **dgAttributeCategory.SelectedIndex = i;** else dgAttributeCategory.EditItemIndex = i; boolItHasBeenDeleted = false; break; } else { i++; } } if (boolItHasBeenDeleted){//Warn the user} dgAttributeCategory.DataBind();
Thanks, WillActually, I've managed to humiliate myself. My problem was that my code was in the ItemCommand handler instead of the SelectedIndexChanged handler. Finally found that in "Programming Microsoft ASP.NET". In it Dino Esposito sez that the value of SelectedIndex isn't set to the new value until the SelectedIndexChanged event is fired. Yet another case of RTFM saving the day. Thanks Christian but I believe you are mistaken. Sheepishly, Will