How to store list

  • Thread starter Thread starter martin
  • Start date Start date
M

martin

Hi,

I would appreciate knowing the best way to store a two dimensional array,
and the bind that array to a dropdownlist.

The original array is in the form

integer, string

and the list will look like this

1,"sdfsdf"
2,"dsfsdf"
3,"gfhfghgfhfg"

The integer will eventually bind to the datavalue field of the dropdown.
The string will eventually bind to the dataText field of the dropdown.

I know I could bing directly to a dataset although I have three instance of
the same dropdown on the page.
so I figured fill an array once from a database and the populating the three
dropdowns from an array, is better then going to the db 3 times for the same
information.

can anybody please point me in the correct direction.

cheers

martin.
 
Hey Martin,

I wonder if a Hashtable is what you want here? It seems pretty easy to jam
the data into it using the Add method and then assign the Hashtable to the
ddl. Here's some code:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim hsh As New Hashtable
hsh.Add(1, "sdfsdf")
hsh.Add(2, "dsfsdf")
hsh.Add(3, "gfhfghgfhfg")
DropDownList1.DataSource = hsh
DropDownList1.DataTextField = "Value"
DropDownList1.DataValueField = "Key"
DropDownList1.DataBind()
' More on this here:
'
http://it.maconstate.edu/tutorials/ASPNET/ASPNET07/aspnet07-02.aspx
End Sub

Does this help?

Ken
Microsoft MVP [ASP.NET]
 
Hi ken,

your hashtable idea is good,

however since my last post I have created an arraylist like this

Public Class Region
Private m_ID As Integer
Private m_Text as String

Public Sub New(ByVal intID As Integer, ByVal strText As String)
m_ID = intID
m_Text = strText
End Sub

End Class

Public Function GetRegionList() As ArrayList
Dim ds As New DataSet
Dim Regions As Region
Dim arrList As New ArrayList
Try
ds = SqlHelper.ExecuteDataset(strConn(),
CommandType.StoredProcedure, "spRegionsGetAll")
Catch ex As Exception
System.Web.HttpContext.Current.Trace.Write("**ERROR***",
ex.Message.ToString)
Throw (ex)
End Try
If Not ds Is Nothing Then
Dim i As Integer
For i = 0 To ds.Tables(0).Rows.Count
arrList.Add(New Region(ds.Tables(0).Rows(0).Item(0),
ds.Tables(0).Rows(0).Item(0)))
Next
End If
Return arrList
End Function

what I would like to do is loop through the arraylist and access each of the
elements like so

Public Shared Function Bind(ByRef cmb As
System.Web.UI.WebControls.DropDownList, ByVal arrList As ArrayList)
Dim i As Integer
For i = 0 To arrList.Count
'cmb.Items.Insert(0, New
System.Web.UI.WebControls.ListItem("Please Select", "0"))
cmb.Items.Insert(0, New
System.Web.UI.WebControls.ListItem(arrList??????, arrList??????))
Next
End Function

I think your solution is better bu would luv to know how to get the result
above.

cheers

martin
















Ken Cox said:
Hey Martin,

I wonder if a Hashtable is what you want here? It seems pretty easy to jam
the data into it using the Add method and then assign the Hashtable to the
ddl. Here's some code:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim hsh As New Hashtable
hsh.Add(1, "sdfsdf")
hsh.Add(2, "dsfsdf")
hsh.Add(3, "gfhfghgfhfg")
DropDownList1.DataSource = hsh
DropDownList1.DataTextField = "Value"
DropDownList1.DataValueField = "Key"
DropDownList1.DataBind()
' More on this here:
'
http://it.maconstate.edu/tutorials/ASPNET/ASPNET07/aspnet07-02.aspx
End Sub

Does this help?

Ken
Microsoft MVP [ASP.NET]


martin said:
Hi,

I would appreciate knowing the best way to store a two dimensional array,
and the bind that array to a dropdownlist.

The original array is in the form

integer, string

and the list will look like this

1,"sdfsdf"
2,"dsfsdf"
3,"gfhfghgfhfg"

The integer will eventually bind to the datavalue field of the dropdown.
The string will eventually bind to the dataText field of the dropdown.

I know I could bing directly to a dataset although I have three instance
of
the same dropdown on the page.
so I figured fill an array once from a database and the populating the
three
dropdowns from an array, is better then going to the db 3 times for the
same
information.

can anybody please point me in the correct direction.

cheers

martin.
 
Back
Top