Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Setting of DataGrid's SelectedIndex appears to be ignored

Setting of DataGrid's SelectedIndex appears to be ignored

Scheduled Pinned Locked Moved ASP.NET
cssquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    OldDog Net
    wrote on last edited by
    #1

    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 to dgAttributeCategory.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

    C O 2 Replies Last reply
    0
    • O OldDog Net

      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 to dgAttributeCategory.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

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      DataBind 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++

      1 Reply Last reply
      0
      • O OldDog Net

        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 to dgAttributeCategory.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

        O Offline
        O Offline
        OldDog Net
        wrote on last edited by
        #3

        Actually, 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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups