Datagrid Dropdown list - setting initial value problems
-
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 FunctionI 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
-
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 FunctionI 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
-
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
-
Hi, I am calling the getselectedindex function after I've populated the dropdown. Not sure where I could be going wrong here!
Rob
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
-
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 FunctionI 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
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
-
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