Search   Articles   Dev Forums   Personalize   Favorites   Member Login   ASP.Net Hosting
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Sunday, February 19, 2006

Search Directory
ESET
ASP.NET
VB.Net
C-Sharp
SQL Server
 
More Partners >>

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
Name:  Sachin 
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?



Disclaimer - Privacy
Copyright © 2008 DevASP.net