Rexx
asked on
Need help with preselecting item from within a dropdownlist.
I am using the following functions to edit a datagrid using a dropdownlist. I want the dropdownlist to preselect by matching to the item that is already in the database to the item within the dropdownlist. For some reason, I have one dropdownlist that does not preselect by matching but rather defaults to the first item in the list.
This function does what it is suppose to do in that the loop preselects the current database item from within the dropdownlist:
Function GetName() as DataSet
ddlDataSet4 = New DataSet
Const StrSQLDDL1 as String = "Select Distinct UserName from budget_people_name_view order by UserName"
Dim myDataAdapter1 as SqlDataAdapter = New SqlDataAdapter(strSQLDDL1, myConnection1)
myDataAdapter1.Fill(ddlDat aSet4, "budget_people_name_view")
Return ddlDataSet4
End Function
Function GetSelName(CatID as String) as Integer
Dim iLoop as Integer
Dim dt as DataTable = ddlDataSet4.Tables("budget _people_na me_view")
For iLoop = 0 to dt.Rows.Count - 1
If CatID.ToLower = (dt.Rows(iLoop)("UserName" )).ToStrin g.ToLower then
Return iLoop
End If
Next iLoop
End Function
For some reason these functions, which are very similar to the ones above with exception in that it joins and displays two columns together within the dropdownlist, yet the dropdownlist always defaults to the first item rather than preselecting the item from what is already in the database:
Function GetSub() as DataSet
ddlDataSet1 = New DataSet
'Populate the ddlDataSet1
Const StrSQLDDL2 as String = "Select Distinct Sub, Sub + ' - ' + Descr As subdescr From SubAcct order by Sub"
Dim myDataAdapter2 as SqlDataAdapter = New SqlDataAdapter(strSQLDDL2, myConnection)
myDataAdapter2.Fill(ddlDat aSet1, "SubAcct")
Return ddlDataSet1
End Function
Function GetSelSub(CatID as String) as Integer
Dim iLoop as Integer
Dim dt as DataTable = ddlDataSet1.Tables("SubAcc t")
For iLoop = 0 to dt.Rows.Count - 1
If CatID.ToLower = (dt.Rows(iLoop)("sub")).To String.ToL ower then
Return iLoop
End If
Next iLoop
End Function
Any clue?
This function does what it is suppose to do in that the loop preselects the current database item from within the dropdownlist:
Function GetName() as DataSet
ddlDataSet4 = New DataSet
Const StrSQLDDL1 as String = "Select Distinct UserName from budget_people_name_view order by UserName"
Dim myDataAdapter1 as SqlDataAdapter = New SqlDataAdapter(strSQLDDL1,
myDataAdapter1.Fill(ddlDat
Return ddlDataSet4
End Function
Function GetSelName(CatID as String) as Integer
Dim iLoop as Integer
Dim dt as DataTable = ddlDataSet4.Tables("budget
For iLoop = 0 to dt.Rows.Count - 1
If CatID.ToLower = (dt.Rows(iLoop)("UserName"
Return iLoop
End If
Next iLoop
End Function
For some reason these functions, which are very similar to the ones above with exception in that it joins and displays two columns together within the dropdownlist, yet the dropdownlist always defaults to the first item rather than preselecting the item from what is already in the database:
Function GetSub() as DataSet
ddlDataSet1 = New DataSet
'Populate the ddlDataSet1
Const StrSQLDDL2 as String = "Select Distinct Sub, Sub + ' - ' + Descr As subdescr From SubAcct order by Sub"
Dim myDataAdapter2 as SqlDataAdapter = New SqlDataAdapter(strSQLDDL2,
myDataAdapter2.Fill(ddlDat
Return ddlDataSet1
End Function
Function GetSelSub(CatID as String) as Integer
Dim iLoop as Integer
Dim dt as DataTable = ddlDataSet1.Tables("SubAcc
For iLoop = 0 to dt.Rows.Count - 1
If CatID.ToLower = (dt.Rows(iLoop)("sub")).To
Return iLoop
End If
Next iLoop
End Function
Any clue?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
By the way, I have a datagrid paging problem I need help with. I am posting this in this forum as subject:
Need help with changing views on the fly that cause datagrid paging problems.
As good as you are I'm sure you can help me solve this one. At least I'm hopeful you will take a stab at it.
Thanks again.
Need help with changing views on the fly that cause datagrid paging problems.
As good as you are I'm sure you can help me solve this one. At least I'm hopeful you will take a stab at it.
Thanks again.
Rexx ,
Glad to help.
OK, i'll take a look there.
Regards
x_com
Glad to help.
OK, i'll take a look there.
Regards
x_com
ASKER
I had to change and add a few things to your code. Both dvwSub and dvwUser were not defined so I did so. Also, made changes to dvwSub=ddlDataSet1.Tables(
as follows
Dim dvwSub as DataTable = ddlDataSet1.Tables("SubAcc
I made a few other changes that I cannot recall so here are the revised functions that work!:
Function GetSub() as DataSet
ddlDataSet1 = New DataSet
'Populate the ddlDataSet1
Const StrSQLDDL2 as String = "Select Distinct Sub, Sub + ' - ' + Descr As subdescr From SubAcct order by Sub"
Dim myDataAdapter2 as SqlDataAdapter = New SqlDataAdapter(strSQLDDL2,
myDataAdapter2.Fill(ddlDat
Dim dvwSub as DataTable = ddlDataSet1.Tables("SubAcc
Return ddlDataSet1
End Function
Function GetSelSub(CatID as String) as Integer
Dim conP As SqlConnection
Dim dstP As DataSet
Dim dadP As SqlDataAdapter
Dim strSQL As String
conP = New SqlConnection("Server= ? ;uid=sa;pwd=;database= ? ")
strSQL = "Select Sub, sub as catid From SubAcct where sub=@catID"
dstP = New DataSet
dadP = New SqlDataAdapter(strSQL, conP)
conP.Open()
dadP.SelectCommand.Paramet
dadP.Fill(dstP, "SubAcct")
Dim dtP As DataTable = dstP.Tables("SubAcct")
Dim iLoop as Integer
Dim dvwUser as DataTable = ddlDataSet1.Tables("SubAcc
For iLoop = 0 To dvwUser.Rows.Count - 1
If Trim(dvwUser.Rows(iLoop)("
Return iLoop
Exit For
End If
Next
End Function
I did not use your datagrid form suggestion since mine works fine as follows:
<asp:boundcolumn DataField="Sub" HeaderText="Sub" SortExpression="Sub" ReadOnly="true"></asp:boun
<asp:templatecolumn>
<edititemtemplate>
<asp:dropdownlist ID="sub1" runat="server" DataValueField="Sub" DataTextField="subdescr"
DataSource="<%# GetSub() %>" SelectedIndex='<%# GetSelSub(Container.DataIt
</edititemtemplate>
</asp:templatecolumn>
<asp:boundcolumn DataField="SubDescr" HeaderText="Sub Descrip'n" SortExpression="SubDescr" ReadOnly="true"></asp:boun
Thanks for all your HELP!!!!!!!! Great job!!!!!!!!!!!