Convert datatable into an arraylist
This article shows how to convert a datatable into an arraylist. Suppose we have a DataTable and we want to copy the data from a particular datatable's specific column to ArrayList, then do as follows.
First, we construct the DataTable and next, we convert the particular DataTable's column values to Arraylist.
namespace CollectionsWebProject { public partial class _Default : System.Web.UI.Page { string connectionString = "Data Source=LocalHost; Initial CataLog=ERPFinAccounting; User ID=kris; Password=XXX"; protected void Page_Load(object sender, EventArgs e) { ArrayList myArrayList = ConvertDataTableToArrayList(); string str = string.Empty; foreach (string name in myArrayList) { str += name + "\n"; } }
The below code creates a simple datatable of two columns: AccCode and AccName and adds two rows to the datatable.
public ArrayList ConvertDataTableToArrayList() { DataTable dtAccounts = new DataTable(); DataColumn dataCol1 = new DataColumn("AccCode"); DataColumn dataCol2 = new DataColumn("AccName"); dtAccounts.Columns.Add(dataCol1); dtAccounts.Columns.Add(dataCol2); dtAccounts.Rows.Add("A001", "Capital Account"); dtAccounts.Rows.Add("A002", "Expense Account"); }
The below code creates an ArrayList. Uses two for loops - one for..loop iterates through each row of the datatable and another loop iterates each column of the selected row.
Adds the column values to the ArrayList. Before adding the column values, we need cast it to a string type by using ToString() method and assign them to arraylist.
ArrayList myArrayList = new ArrayList(); for (int i = 0; i <= dtAccounts.Rows.Count - 1; i++) { for (int j = 0; j <= dtAccounts.Columns.Count - 1; j++) { myArrayList.Add(dtAccounts.Rows[i][j].ToString()); } } return myArrayList; }
ArrayList Articles
- remove items from arraylist
- find item in arraylist using indexof method
- bind arraylist with gridview control in asp net application
- how to bind arraylist with dropdownlist in asp net application
- how to bind arraylist with radiobuttonlist in asp.net application
- convert arraylist to string array
- convert string array to arraylist
- convert dataset to arraylist in asp.net using c#
- how to retrieve arraylist items in reverse order
- difference between arraylist and list collection
- Arrays vs ArrayList
- how to create and add items into the ArrayList in csharp
- how to add items to the arraylist using an add method
- retrieve arraylist items using enumerators
- retrieve arraylist items using for each loop
- retrieve arraylist items using indexers
- insert items to the arraylist using insert and insertrange method
Most Viewed
Azure Q & A
Azure Platform
Grid-View
GridView CommandField example
Details-View
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Entity Framework
Query entity data model using linq to entities
Array List
Difference between arraylist and list collection
Web Services
How to create a Web service using Visual Studio.net
Form-View
FormView DataBound Event
Object Oriented Programming
Calling base class constructor in C#
Linq
Convert a sequence to a generic list using ToList()method
Project Ideas
Project ideas for students
AccountingSoftware
Accounting Software
MVC
Creating an ASP.Net MVC 3 application
.Net
Using assemblies in .net applications
ASP .Net
How to implement form validation using ASP.Net 2.0 Validation Controls