How to bind DropdDownList to ArrayList

P

Peter Afonin

Hello,

I have a code that queries Active Directory and put logins and names into
the ArrayList. Then I need to bind this ArrayList to the DropdDownList,
making names as DataTextField and logins as DataValueFields.

I have no problem doing it, if I have only logins or only names, since
ArrayList always has one dimension. But I need them both. So the code looks
like this:

Sub Getallusers(ByVal selddl As DropDownList)
Try
Dim sAdsPath As String = "LDAP://seart01/dc=acdtraining,dc=com"
Dim de As New DirectoryEntry(sAdsPath, "Domain\user",
"password")
Dim ds1 As New DirectorySearcher(de)
ds1.Filter = "(objectCategory=person)"
ds1.Filter = "(objectClass=user)"
ds1.Filter = "(employeeType=ALT)"
'Dim colServers(100, 100) As String
Dim colServers As ArrayList
colServers = New ArrayList
Dim result As SearchResult

For Each result In ds1.FindAll()
colServers.Add(New String()
{result.Properties("displayName")(0),
result.Properties("samAccountName")(0)})
Next result
'colServers.Sort()
selddl.DataSource = colServers

??????????????????????????????????

selddl.DataBind()
de.Dispose()
Catch ex As Exception
Dim s As String = ex.Message
End Try
End Sub

The ArrayList is filling up OK, now each value contains two values - Name
(0) and Login(1). But how to extract them separately and make one a text
field and another - value field? Tried everything, but couldn't fugure out.
It's definitely possible, since each value has it's own index (0 or 1), but
I don't know how.

Alternatively, how would I use just an Array instead?

I would appreciate your help very much.

Thank you,
 
A

Alan Samet

Here's a reference example that uses a similar technique:

Sub PopulateDropDownList(ByVal ddl As DropDownList)
Dim al As New ArrayList()
For i As Integer = 0 To 10
al.Add(New LoginClass("Login: " & i.ToString(), "Name: " &
i.ToString()))
Next
ddl.DataSource = al
ddl.DataTextField = "Login"
ddl.DataValueField = "Name"
ddl.DataBind()
End Sub

Class LoginClass
Public Readonly Property Name() As String
Get
Return _name
End Get
End Property

Public Readonly Property Login() As String
Get
Return _login
End Get
End Property


Private _login As String
Private _name As String
Public Sub New(ByVal login As String, ByVal name As String)
_login = login
_name = name
End Sub
End Class

Hope I've helped.

-Alan
 
P

Peter Afonin

Thank you very much, Alan. I've heard that creating a class would be a best
solution, but wasn't sure how to do this.

Peter
 
C

Cor Ligthert [MVP]

Peter,

You add an array of two strings to your arraylist, therefore probably what
you want is to get the first two arraylist items.

\\\
dim mystring1 as string = directcast(arraylist(0),string())(0)
and
dim mystring2 as string = directcast(arraylist(0),string())(1)
///
Roughly typed not tested.

I hope this helps,

Cor
 
C

Cor Ligthert [MVP]

Peter,

A much easier solution is building a datatable and use that (has as well not
so much overhead by extra classes, if needed sorting etc because all code is
build in standard in the system.net namespace for that).

\\\
dim dt as datatable
dt.columns.add("person")
dt.columns.add("user")
for each in................
dt.rows.add(dt.newrow)
dt.rows(0) = first item
dt.rows(1) = second itme
next
drpdown.datasource = dt
drpdown.datatextfield = "person"
drpdown.datavalufield = "user"
drpdown.databind
////

I hope this helps,

Cor
 
A

Alan Samet

Ha -- funny I didn't think of it earlier. You may also use the
sortedlist or hashtable classes to accomplish key-value databinding as
well.

example:

Sub PopulateDropDownList(ByVal ddl As DropDownList)
Dim sl As New SortedList() 'Hashtable()
For i As Integer = 0 To 10
sl.Add("Login: " & i.ToString(), "Name: " & i.ToString())
Next
ddl.DataSource = sl
ddl.DataTextField = "Key"
ddl.DataValueField = "Value"
ddl.DataBind()
End Sub
 
P

Peter Afonin

Thank you very much, Cor. I'll try it your way. I've tried before something
similar, but messed something up.

Peter

Cor Ligthert said:
Peter,

A much easier solution is building a datatable and use that (has as well not
so much overhead by extra classes, if needed sorting etc because all code is
build in standard in the system.net namespace for that).

\\\
dim dt as datatable
dt.columns.add("person")
dt.columns.add("user")
for each in................
dt.rows.add(dt.newrow)
dt.rows(0) = first item
dt.rows(1) = second itme
next
drpdown.datasource = dt
drpdown.datatextfield = "person"
drpdown.datavalufield = "user"
drpdown.databind
////

I hope this helps,

Cor
 
P

Peter Afonin

Cor, with a few modifications your solution worked great for me, thank you
very much.

Dim result As SearchResult
Dim dt As DataTable = New DataTable
dt.Columns.Add("Name")
dt.Columns.Add("Login")
Dim n As Integer = 0

For Each result In ds1.FindAll()
dt.Rows.Add(dt.NewRow)
dt.Rows(n)("Name") = result.Properties("displayName")(0)
dt.Rows(n)("Login") = result.Properties("samAccountName")(0)
n = n + 1
Next result

Dim dv As DataView = dt.DefaultView
dv.Sort = "Name"

selddl.DataSource = dv
selddl.DataTextField = "Name"
selddl.DataValueField = "Login"
selddl.DataBind()

Peter

Cor Ligthert said:
Peter,

A much easier solution is building a datatable and use that (has as well not
so much overhead by extra classes, if needed sorting etc because all code is
build in standard in the system.net namespace for that).

\\\
dim dt as datatable
dt.columns.add("person")
dt.columns.add("user")
for each in................
dt.rows.add(dt.newrow)
dt.rows(0) = first item
dt.rows(1) = second itme
next
drpdown.datasource = dt
drpdown.datatextfield = "person"
drpdown.datavalufield = "user"
drpdown.databind
////

I hope this helps,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top