Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to Bind ArrayList to DataList Control

Author: DevASP Team
Download Source Code : 707_Datalist.zip

In this article I will try to explain you how you can bind arraylist to datalist control. Here I will create application in ASP.Net 2.0 and C#

Often I have seen queries posted on different forums about binding arraylist to datalist control. So by research I reached to solution and decided to write article so that it can help beginners in solving problem. To begin with drop the datalist control, in ItemTemplate tag add the Label control and bind its text to the column table column the code of you aspx page will be look like as follows:

 

        <asp:DataList ID="DataList1" runat="server">

            <ItemTemplate>

                <asp:Label ID="lblalobject" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "al object") %>'></asp:Label>

            </ItemTemplate>

        </asp:DataList>    

 

Now on Page load event create new ArrayList object (if ArrayList is not assessable check you have imported System.Collections namespace) add items in arraylist using arraylist add method. Now convert this arraylist to dataset by adding new table and defining new column. This chunk of code will be look like as follows:

 

        DataSet ds = new DataSet();

        ds.Tables.Add("al");

        ds.Tables[0].Columns.Add("al object");

 

        foreach(string str in al)

        {

            if (str != "")

            {

                DataRow dr = ds.Tables[0].NewRow();

                dr["al object"] = str;

                ds.Tables[0].Rows.Add(dr);

            }       

        }

 

Now in end simply bind the dataset object to datalist datasource. Your final code will be look like as follows: (Note my whole code is on page_load event)

 

    protected void Page_Load(object sender, EventArgs e)

    {

        ArrayList al = new ArrayList();

 

        for (int i = 0; i < 10; i++)

        {

            al.Add("Array List Object" + i.ToString());

        }

 

 

        DataSet ds = new DataSet();

        ds.Tables.Add("al");

        ds.Tables[0].Columns.Add("al object");

 

        foreach(string str in al)

        {

            if (str != "")

            {

                DataRow dr = ds.Tables[0].NewRow();

                dr["al object"] = str;

                ds.Tables[0].Rows.Add(dr);

            }       

        }

 

 

        DataList1.DataSource = ds;

        DataList1.DataBind();

      

 

    }

Article Comments
I've maked an arraylist object & i wanna add the dataset in this arraylist object.In the dataset i've retriew a table from Mydatabase.
Now i wanna set the property of DataGrid control DataGrid1.dataSource=arraylist objecthow can I?

Posted on 2/12/2008 3:31:25 AM by Sachin

now just bound the column/s of DataGrid with the object of the Arraylist you created.

Posted on 9/26/2008 3:16:15 AM by vishal Vashishta

hi
i am manikyaprabhu
i want code for the floating popup window using asp.net 2.0

Posted on 12/4/2008 2:00:36 AM by manikya parbhu

hi sir
i working on the datalist control in asp.net
how you use generices in that and how to bind the data to the datalist?

Posted on 12/4/2008 2:02:47 AM by manikya parbhu

how do i enable paging in a datalist

Posted on 7/8/2010 4:07:09 AM by julius

i have making arraylist items are bound to the datalist..i need help how to bind arraylist in datalist

Posted on 12/2/2010 10:34:41 AM by sivakumar

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to Merge DataSets using C#

Disclaimer - Privacy
© 2002-2017 DevASP.net