Link to home
Start Free TrialLog in
Avatar of Rexx
RexxFlag for United States of America

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(ddlDataSet4, "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_name_view")
      For iLoop = 0 to dt.Rows.Count - 1
      If CatID.ToLower = (dt.Rows(iLoop)("UserName")).ToString.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(ddlDataSet1, "SubAcct")
      Return ddlDataSet1
  End Function

  Function GetSelSub(CatID as String) as Integer
        Dim iLoop as Integer
      Dim dt as DataTable = ddlDataSet1.Tables("SubAcct")
      For iLoop = 0 to dt.Rows.Count - 1
      If CatID.ToLower = (dt.Rows(iLoop)("sub")).ToString.ToLower then
          Return iLoop
      End If
      Next iLoop
  End Function

Any clue?
ASKER CERTIFIED SOLUTION
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rexx

ASKER

Actually, I only had one problem.  The first problem you identified, those functions work.  But the value was always string and did not have the dual column display in the dropdownlist data field.  

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("SubAcct").DefaultView
as follows
Dim dvwSub as DataTable = ddlDataSet1.Tables("SubAcct")

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, myConnection)    
   myDataAdapter2.Fill(ddlDataSet1, "SubAcct")
   Dim dvwSub as DataTable = ddlDataSet1.Tables("SubAcct")
   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.Parameters.Add("@catID", CatID)
    dadP.Fill(dstP, "SubAcct")

    Dim dtP As DataTable = dstP.Tables("SubAcct")

    Dim iLoop as Integer
    Dim dvwUser as DataTable = ddlDataSet1.Tables("SubAcct")
      For iLoop = 0 To dvwUser.Rows.Count - 1
     If Trim(dvwUser.Rows(iLoop)("Sub")) = Trim(dtP.Rows(0)("Sub")) Then
         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:boundcolumn>
        <asp:templatecolumn>
        <edititemtemplate>
        <asp:dropdownlist ID="sub1" runat="server" DataValueField="Sub" DataTextField="subdescr"
                          DataSource="<%# GetSub() %>" SelectedIndex='<%# GetSelSub(Container.DataItem("sub")) %>' />
        </edititemtemplate>
        </asp:templatecolumn>
      <asp:boundcolumn DataField="SubDescr" HeaderText="Sub Descrip'n" SortExpression="SubDescr" ReadOnly="true"></asp:boundcolumn>


Thanks for all your HELP!!!!!!!!  Great job!!!!!!!!!!!
Avatar of Rexx

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.
Rexx ,
Glad to help.
OK, i'll take a look there.

Regards
x_com