Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Item index change in a drop down list (ddl) using VB.Net

Posted on 2004-04-15
43
Medium Priority
?
655 Views
Last Modified: 2012-05-04
In VB.Net, how can I change the index (0, 1, 2, 3...) of an item of a drop down list (ddl)?
My case: I want to change a specific item's index to "0" so it is the first in list.
p.s. Don't tell me to use Properties/Misc/Items because that wont work in this case is not that simple.  If anymore details are needed please let me know.
0
Comment
Question by:wmorales
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 22
  • 14
  • 4
  • +1
43 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10835984
well one example would be to just move it in the items collection

tmp = ddl.Items[0]
ddl.Items[0] = ddl.Items[5]
ddl.Items[5] = tmp
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 10836117
is your ddl bound to some datasource?
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10836128
true I should have asked that if it is just change the index in the bound collection ...
0
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

Author Comment

by:wmorales
ID: 10836198
and I would declare the tmp variable as what
0
 

Author Comment

by:wmorales
ID: 10836222
yes is bound to a datasource but the last field the one I want to make first isn't I added after the databind, ddl.Items.Add("...")
0
 

Author Comment

by:wmorales
ID: 10836236
well yes the index of the last item of the collection to 0
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10836237
either object is fine for a generic move (you are only moving references not accessing the type) or whatever is in items ... (if binding you put your own classes)
0
 

Author Comment

by:wmorales
ID: 10836317
I kind of get your answer but to be sure ---> dim tmp as ?
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 10836330
if you don't kow how to dim your var , always dim it as Object. It is a little overhead, but it will work
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10836381
unless you are accessing properties :) then thats evil.
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 10836392
true
:(
0
 

Author Comment

by:wmorales
ID: 10836496
Sub FILLddl()
        Dim con As SqlConnection
        con = New SqlConnection("Server=localhost;uid=sa ;pwd= ; database=DATABASE")
        con.Open()

        Dim sql As String
        sql = "SELECT * FROM TABLE"

        Dim da As New SqlDataAdapter(sql, con)
        Dim ds As New DataSet

        da.Fill(ds, "DATABASE")

        ddl.DataSource = ds
        ddl.DataTextField = "TABLE"
        ddl.DataValueField = "FIELD"

        ddl.DataBind()

        'ITEM THAT I WANT TO CHANGE INDEX TO 0
        ddl.Items.Add("...")
End Sub
0
 

Author Comment

by:wmorales
ID: 10836526
and I do this because I can't have the "..." in the database
0
 

Author Comment

by:wmorales
ID: 10836543
or don't want to have... either way that’s that
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 10836661
You can make this ddl hidden and
create another unbound ddl where you will add your item and then everything from the hidden ddl
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10836678
please excuse C# code ...

            public static void FillDropDown(System.Web.UI.WebControls.DropDownList _ddl, string _Table, string _DisplayColumn, string _ValueColumn)
            {
                  string ConnectionString = EnterpriseObjects.EnterpriseApplication.Application.ConnectionString ;
                  SqlConnection Connection = new SqlConnection(ConnectionString);

                  SqlCommand objCommand = new SqlCommand("SELECT " + _DisplayColumn+ " as disp," + _ValueColumn + " as val FROM " + _Table, Connection);

                  Connection.Open();
                                                _ddl.Items.Add(new ListItem("...", "...")
                  SqlDataReader objReader = objCommand.ExecuteReader();
                  while(objReader.Read()) {
                        _ddl.Items.Add(new ListItem(objReader.GetValue(0).ToString(), objReader.GetValue(1).ToString()));
                  }
                  objReader.Close();
                  Connection.Close();
            }
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10836691
not the prettiest code but it will do exactly what you want ....
0
 

Author Comment

by:wmorales
ID: 10836715
I'm going to try the hidden concept first then look into the C# algorism.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10836739
just add them to the Items collection by yourself thats all its doing

0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10836762
Sub FILLddl()
        Dim con As SqlConnection
        con = New SqlConnection("Server=localhost;uid=sa ;pwd= ; database=DATABASE")
        con.Open()

        Dim sql As String
        sql = "SELECT * FROM TABLE"

        Dim da As New SqlDataAdapter(sql, con)
        Dim ds As New DataSet

        da.Fill(ds, "DATABASE")

        DataTable dt = ds.Tables("DATABASE")
        ddl.Items.Add(new ListItem("...", "..."))
        for i = 0 to dt.Rows.Count - 1
             ddl.Items.Add(new ListItem(dt.Rows(i)(whatever),dt.Rows(i)(whatever))
        end for

        ddl.DataBind()

        'ITEM THAT I WANT TO CHANGE INDEX TO 0
        ddl.Items.Add("...")
End Sub
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10836763
oops remove databind call ...
0
 

Author Comment

by:wmorales
ID: 10836807
I know I did that in another way:

Do While objDataReader.Read() = True
            strResultsHolder = objDataReader("FIELD")
            ddl.Items.Add(strResultsHolder)
Loop

I just want to have it without do's or for's
0
 

Author Comment

by:wmorales
ID: 10836814
I know... :)
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10836854
what do you think the databinding does ? :)
0
 

Author Comment

by:wmorales
ID: 10836936
More complete:

Sub FILLddl()

        Dim con As SqlConnection
        con = New SqlConnection("Server=localhost;uid=sa ;pwd= ; database=DATABASE")
        con.Open()

        Dim strSQL As String = "SELECT * FROM TABLE"
        Dim strResultsHolder As String

        Dim objCommand As New SqlCommand(strSQL, conINVENTORY)
        Dim objDataReader As SqlDataReader

        objDataReader = objCommand.ExecuteReader()

        'HERE THE ITEM'S INDEX IS 0 BUT IS NOT WHAT I WANT
        'THE WAY I SHOW U BEFORE IS WHAT I WANT... I KNOW I'M LOOKING FOR...
        '... TROUBLE WHERE THERE ISN'T ANY BUT HEY...
        ddlWORKPLACE_NAME.Items.Add("...")

        Do While objDataReader.Read() = True
            strResultsHolder = objDataReader("TABLE")
            ddL.Items.Add(strResultsHolder)

            'ddl.Fill(ds, "TABLE")
        Loop

End Sub
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 10836953
if you use dataset and dataadapter just to retrieve data from the db why dont you use

Sub FILLddl()
        Dim con As SqlConnection
        con = New SqlConnection("Server=localhost;uid=sa ;pwd= ; database=DATABASE")
        con.Open()

        Dim sql As String
        sql = "SELECT * FROM TABLE"

        Dim da As New SqlDataAdapter(sql, con)
        Dim ds As New DataSet

        da.Fill(ds, "DATABASE")

        Dim dr as DataRow = ds.Tables(0).NewRow()
        ds.Tables(0).Rows.InsertAt( dr, 0 )

        ddl.DataSource = ds
        ddl.DataTextField = "TABLE"
        ddl.DataValueField = "FIELD"

        ddl.DataBind()

        'ITEM THAT I WANT TO CHANGE INDEX TO 0
        ddl.Items.Add("...")
End Sub

HTH
NSub FILLddl()
        Dim con As SqlConnection
        con = New SqlConnection("Server=localhost;uid=sa ;pwd= ; database=DATABASE")
        con.Open()

        Dim sql As String
        sql = "SELECT * FROM TABLE"

        Dim da As New SqlDataAdapter(sql, con)
        Dim ds As New DataSet

        da.Fill(ds, "DATABASE")

        ddl.DataSource = ds
        ddl.DataTextField = "TABLE"
        ddl.DataValueField = "FIELD"

        ddl.DataBind()

        'ITEM THAT I WANT TO CHANGE INDEX TO 0
        ddl.Items.Add("...")
End Sub


HTH
B..M
0
 

Author Comment

by:wmorales
ID: 10836973
is: objDataReader("FIELD") not TABLE
and the: 'ddl.Fill(ds, "TABLE") at the end is a comment ignore that
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 10837015
sorry, i've missed it
just see if you can get something from the posted code - it is exactly what you want in your question.
I applogies that i didn't read all the posts carefully and i was missed the correction

B..M
0
 

Author Comment

by:wmorales
ID: 10837088
mmarinov
I tried what you said but what it does is that gives a blank item ("     '') that does have the index 0 but I don’t what a blank in my ddl and is not the item that I want to have it's index 0
0
 

Author Comment

by:wmorales
ID: 10837103
Don’t worry I did the same today with somebody else problem.
0
 

Author Comment

by:wmorales
ID: 10837245
There isn't any other way to change the index other from the solutions we already have isn’t... because with the hidden idea it would work yes, but then it hit me that what I have to do for that to work is the same thing a wanted to omit whit the original ddl in the first place... but thank you anyway.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10837305
items.insert()

Public Overridable Sub Insert( _
   ByVal index As Integer, _
   ByVal item As Object _
) Implements IList.Insert

0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 2000 total points
ID: 10837312
sorry didnt mean to submit ... ddl.Items.Insert(0, item) will put it at the beginning.
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 10837369
yes my sample add a blank item to your ddl, because i don't know what exactly you want to add - it is just the mechanism
you can use dr(index) od dr(nameofcolumn) and set the needed value to it so when the ddl is binded you will receive the dr at index 0 with the appropriate value and text

B..M
0
 

Author Comment

by:wmorales
ID: 10837390
YOU GOT IT!!!
0
 

Author Comment

by:wmorales
ID: 10837393
Yes I know I figured it out
0
 

Author Comment

by:wmorales
ID: 10837407
So what do I do now do I press Accept on gregoryyoung's
answer?
0
 

Author Comment

by:wmorales
ID: 10837414
Yes?
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10837431
I dunno I have never asked a question :)
0
 

Author Comment

by:wmorales
ID: 10837468
In a nutshell it all comes down to using Insert and not Add.
0
 

Author Comment

by:wmorales
ID: 10837478
Thank you all, problem solve... NEXT!
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10837483
yep .. although you could still add and flip as earlier stated ...
0
 

Author Comment

by:wmorales
ID: 10837562
Lest stick to the Insert… :)
0

Featured Post

Google Certified Professional - Cloud Architect

This course (1 of 3) is designed to help students who are interested in Google Cloud Platform (GCP) to become familiar with the platform, navigate the console and learn its capabilities. It will also prepare students for the Google Cloud Architect certification exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

688 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