Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     




Array List in Asp.Net 2.0

The ArrayList object is a collection of items containing a single data value. It is not a control but an object. Arraylist members can be added using Add() property

Like

ArrayList ArLst =  new ArrayList ();

ArLst.Add("Xpode");
ArLst.Add("World");
ArLst.Add("NoWayOut");

If need to sort in alphabetically order the we will use the Sort Property
ArLst.Add("Xpode");
ArLst.Add("World");
ArLst.Add("NoWayOut");
ArLst.Sort()

if we want in reverse order then we will use Reverse Property

ArLst.Add("Xpode");
ArLst.Add("World");
ArLst.Add("NoWayOut");
ArLst.Sort()
ArLst.Reverse()

Now if we want to binds this with any thing then we will use this

GridView.DataSourse = ArLst;
GridView.DataBind();

There are many controls like given below who needs to assign the Text and Value Property Sepertatly, But with these controls ArrayList automatically generates the text and values to the following controls: * asp:RadioButtonList * asp:CheckBoxList * asp:DropDownList * asp:Listbox To Binde with these controls you need to just need to change the below code:

GridView.DataSourse = ArLst;
GridView.DataBind();

Change the Id of the control with GridView. By default, an ArrayList object contains 16 entries This was the static example, if we need to fill the arraylist from database dynamically then we need to write some code. The example is given below. This code is in Vb.Net

Dim MyArrayList as ArrayList 	
Dim sItem as String
Sub Page_Load(Source as Object, E as EventArgs)
MyArrayList=New Arraylist
if not Page.IsPostBack then
Dim strConn as string = "server=YourServer;uid=UID;pwd=PWD;database=NorthWind"
Dim MySQL as string = "Select CategoryName from Categories"
Dim MyConn as New SQLConnection(strConn)
Dim objDR as SQLDataReader
Dim Cmd as New SQLCommand(MySQL, MyConn)
MyConn.Open()
objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)

While objDR.Read()
MyArrayList.add(objDR("CategoryName"))
End While
end if
ddl1.datasource=MyArrayList
ddl1.databind

Share this article   |    Print    |    Article read by 3447 times
Author:
Rohit kakria
I am software developer, moderator of xpode.com
Related Articles:
Related Interview Questions: