Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can i store a dropdownlist item into arraylist?
Posted
Comments
Xeshan Ahmed 14-Oct-11 5:03am    
do you want to store values of dropdownlist into arraylist ??
Al Moje 14-Oct-11 5:23am    
Please rephrase you question not clear…

Hi,

check this once

C#
        List<string> str = new List<string>();
        foreach (ListItem sd in ddllist.Items)
        {
            str.Add(sd.Value);
        }
</string></string>


All the Best
 
Share this answer
 
Firstly, don't use an arraylist if you can avoid it - they aren't type safe so they are more work and less readable than they should be. Use a typesafe List<T> instead. That way you don't have to cast the array elements before you use them.

After that, it's pretty simple:
Declare your List<T> with the appropriate type (whatever you have loaded the DropDownList with)
Fill it using the the List.AddRange method, handing it the DropDownList.Items collection or the subset you require.
 
Share this answer
 
// Decalre 2 ArryList to Hold the Text and Value of Items
ArrayList alText= new ArrayList();
ArrayList alValue = new ArrayList();
//Copy the Text field in the First Arry List
foreach (ListItem li in objDDL.Items)
{
alTextt.Add(li.Text);
}

//Copy the value field in the Second Array List based on First ArrayList value
foreach (object item in alText)
{
string val = objDDL.Items.FindByText(item.ToString()).Value;
alValue.Add(val);
}
 
Share this answer
 
C#



Code:

' - - - - - - - - - - - - - - - - - - - - - - - - - - -
' This will get info of certain list in an excel range
' The basis for look up is the cell one row above the first list entry
' (this cell is usually reserved for title of the first column)

Function xlrangeListCount(SheetName As String, RangeName As String, Optional OffsetC = 0) As Long
Dim thisCount As Long
With Sheets(SheetName).Range(RangeName)
thisCount = 0
Do
If Not (IsEmpty(.Offset(thisCount + 1, 0))) Then
thisCount = thisCount + 1
Else
Exit Do
End If
Loop
End With
xlrangeListCount = thisCount
End Function



Function xlrangeListItem(SheetName As String, RangeName As String, OffsetRow As Long, Optional OffsetCol = 0)
xlrangeListItem = Sheets(SheetName).Range(RangeName).Offset(OffsetRow, OffsetCol).Value
End Function


Now, here is an example of using those 2 function to create a dynamic array.

Code:

Sub GetRangeList()
Dim MyList()
Dim i As Long
Dim ListCount As Long

ListCount = xlrangeListCount("Sheet1", "A1")
ReDim MyList(1 To ListCount, 1 To 3)
For i = 1 To ListCount
MyList(i, 1) = xlrangeListItem("Sheet1", "A1", i)
MyList(i, 2) = xlrangeListItem("Sheet1", "A1", i, 1)
MyList(i, 3) = xlrangeListItem("Sheet1", "A1", i, 2)
Next

End Sub

Take note that this code assumes your list have headers(or titles) and the "A1" refered to is the first header in the list.

I assume you can put it (MyList()) in a combobox.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900