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. Datagrid Dropdown list - setting initial value problems

Datagrid Dropdown list - setting initial value problems

Scheduled Pinned Locked Moved ASP.NET
helpdatabaseannouncement
6 Posts 3 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.
  • R Offline
    R Offline
    robw1888
    wrote on last edited by
    #1

    Hi I'm using the following code to try and retrieve the current value of a database field to set that as the initial value of a dropdownlist in an editable datagrid.

    Function GetSelectedIndex(StatusID As String) As Integer
    'Loop through the DataSet DS
    Dim iLoop As Integer
    Dim DT As DataTable = DS.Tables("Statusds")
    For iLoop = 0 To DT.Rows.Count - 1
    If Int32.Parse(StatusID) = Int32.Parse(DT.Rows(iLoop)("StatusID")) Then
    Return iLoop
    End If
    Next iLoop
    End Function

    I get the following error - 'IndexOutOfRangeException: StatusID' when I remove the selectedindex function call I can use the list to update the database so seems to be a problem with my coding to retrieve the current value. The table I'm using has StatusID as the primary key field and 'Status' as the field I'm using for the ddl. any help would be much appreciated! Thanks

    Rob

    K K 2 Replies Last reply
    0
    • R robw1888

      Hi I'm using the following code to try and retrieve the current value of a database field to set that as the initial value of a dropdownlist in an editable datagrid.

      Function GetSelectedIndex(StatusID As String) As Integer
      'Loop through the DataSet DS
      Dim iLoop As Integer
      Dim DT As DataTable = DS.Tables("Statusds")
      For iLoop = 0 To DT.Rows.Count - 1
      If Int32.Parse(StatusID) = Int32.Parse(DT.Rows(iLoop)("StatusID")) Then
      Return iLoop
      End If
      Next iLoop
      End Function

      I get the following error - 'IndexOutOfRangeException: StatusID' when I remove the selectedindex function call I can use the list to update the database so seems to be a problem with my coding to retrieve the current value. The table I'm using has StatusID as the primary key field and 'Status' as the field I'm using for the ddl. any help would be much appreciated! Thanks

      Rob

      K Offline
      K Offline
      kubben
      wrote on last edited by
      #2

      I would guess you are trying to set the SelectedIndex before the dropdown is loaded. You would need to bind the dropdown first and then try to set the SelectedIndex. Hope that helps. Ben

      R 1 Reply Last reply
      0
      • K kubben

        I would guess you are trying to set the SelectedIndex before the dropdown is loaded. You would need to bind the dropdown first and then try to set the SelectedIndex. Hope that helps. Ben

        R Offline
        R Offline
        robw1888
        wrote on last edited by
        #3

        Hi, I am calling the getselectedindex function after I've populated the dropdown. Not sure where I could be going wrong here!

        Rob

        K 1 Reply Last reply
        0
        • R robw1888

          Hi, I am calling the getselectedindex function after I've populated the dropdown. Not sure where I could be going wrong here!

          Rob

          K Offline
          K Offline
          kubben
          wrote on last edited by
          #4

          Well, if you debug into the code and can see what the Getselectedindex is returning, you should be able to look at the items in the drop down and tell if that index exists. I am guessing that somehow the dropdown still isn't bound so the items don't exist yet. Ben

          1 Reply Last reply
          0
          • R robw1888

            Hi I'm using the following code to try and retrieve the current value of a database field to set that as the initial value of a dropdownlist in an editable datagrid.

            Function GetSelectedIndex(StatusID As String) As Integer
            'Loop through the DataSet DS
            Dim iLoop As Integer
            Dim DT As DataTable = DS.Tables("Statusds")
            For iLoop = 0 To DT.Rows.Count - 1
            If Int32.Parse(StatusID) = Int32.Parse(DT.Rows(iLoop)("StatusID")) Then
            Return iLoop
            End If
            Next iLoop
            End Function

            I get the following error - 'IndexOutOfRangeException: StatusID' when I remove the selectedindex function call I can use the list to update the database so seems to be a problem with my coding to retrieve the current value. The table I'm using has StatusID as the primary key field and 'Status' as the field I'm using for the ddl. any help would be much appreciated! Thanks

            Rob

            K Offline
            K Offline
            kapiljadhav
            wrote on last edited by
            #5

            Hi, You have declared iloop as Integer. In dot net if we do not initialize the intiger it will take it as -1 as the initial value. So assign it 0 or 1 as Dim iLoop As Integer = 0 This should work..... Regards, Kaps

            R 1 Reply Last reply
            0
            • K kapiljadhav

              Hi, You have declared iloop as Integer. In dot net if we do not initialize the intiger it will take it as -1 as the initial value. So assign it 0 or 1 as Dim iLoop As Integer = 0 This should work..... Regards, Kaps

              R Offline
              R Offline
              robw1888
              wrote on last edited by
              #6

              Thanks kaps, Was another small problem I needed to sort but your suggestion got me past the initial error so I've got it all working now! Much appreciated!

              Rob

              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