Solved

Need help with preselecting item from within a dropdownlist.

Posted on 2003-11-21
4
216 Views
Last Modified: 2012-05-04
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?
0
Comment
Question by:Rexx
  • 2
  • 2
4 Comments
 
LVL 29

Accepted Solution

by:
David H.H.Lee earned 250 total points
ID: 9801845
Rexx,

Here's some fix about your code :

Instead of DataSet set to global, i would more like to set it to DataView ' because just need to preselect data from a view

your first problems
=========================
Dim dvwUser as DataView ' set it global

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")
   dvwUser=ddlDataSet4.Tables("budget_people_name_view").DefaultView
   Return ddlDataSet4
End Function

 Function GetSelName(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 UserName from budget_people_name_view where catID=@catID"
    dstP = New DataSet
    dadP = New SqlDataAdapter(strSQL, conP)
    conP.Open()
    dadP.SelectCommand.Parameters.Add("@catID", CatID)
    dadP.Fill(dstP, "budget_people_name_view")

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

    Dim iLoop as Integer
    For iLoop = 0 To dvwUser.Count - 1
      If Trim(dvwUser.Item(iLoop).Item("UserName")) = Trim(dtP.Rows(0)("UserName")) Then
          Return iLoop
          Exit For
      End If
    Next
  End Function


your second problems
========================
Dim dvwSub as DataView ' set it global

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")
   dvwSub=ddlDataSet1.Tables("SubAcct").DefaultView
   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=master")
    strSQL = "Select Sub From SubAcct where catID=@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
    For iLoop = 0 To dvwUser.Count - 1
      If Trim(dvwUser.Item(iLoop).Item("Sub")) = Trim(dtP.Rows(0)("Sub")) Then
          Return iLoop
          Exit For
      End If
    Next
 End Function
 
 In first problems,
 -I assume you hold a information from budget_people_name_view.After bind the dataset into datasource dropdownlist, you need to preselect items
 come from DB based on id.
 eg:
 <asp:DropDownList Runat="server" ID="drpUser" DataSource='<%#GetName()%>' DataTextField ="UserName" DataValueField ="UserName" SelectedIndex ='<%#GetSelName(DataBinder.Eval(Container.DataItem, "catID"))%>' >
 </asp:DropDownList>
                                    
                                    
 In second problems, it not more different with first problems.The sample of the databind solutions should be more like this :
-<asp:DropDownList Runat="server" ID="drpSub" DataSource='<%#GetSub()%>' DataTextField ="Sub" DataValueField ="Sub" SelectedIndex ='<%#GetSelSub(DataBinder.Eval(Container.DataItem, "catID"))%>' >
 </asp:DropDownList>
 
 -You can modify it if not match with your tableName/fieldName or somethings else there. What i see from your problems is you're compare somethings with different field.
eg:
 If CatID.ToLower = (dt.Rows(iLoop)("UserName")).ToString.ToLower then
-you compare catID with userName?

I use my code above to solve my problems about pre-select a value from dropdownlist.
 
Regards
x_com
0
 

Author Comment

by:Rexx
ID: 9804453
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!!!!!!!!!!!
0
 

Author Comment

by:Rexx
ID: 9804473
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.
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 9805205
Rexx ,
Glad to help.
OK, i'll take a look there.

Regards
x_com
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now