Click here to Skip to main content
12,635,792 members (26,367 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB ASP.NET VB.NET
Hi , can anyone teach me how to convert data table to arraylist or to loop data table contents, my datatable has only 1 column.
I dont have google sorry :)
Thanks
Posted 16-Jan-14 21:15pm
Goenitz558
Comments
thatraja 17-Jan-14 2:19am
   
Don't have Google? how about bing? or other search engines?
Goenitz 17-Jan-14 2:22am
   
nothing just code project and asp.net :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

ArrayList rows = new ArrayList();

foreach (DataRow dataRow in myDataTable.Rows)
    rows.Add(string.Join(";", dataRow.ItemArray.Select(item => item.ToString())));


It is unwise to use the obsolete ArrayList .Use List<string> instead, since the rows are strings:
This may work.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

//create DataTable
            DataTable dt = new DataTable();
            dt.Columns.Add("Name", typeof(string));
            for (int i = 1; i < 10; i++)
            {
                DataRow dr = dt.NewRow();
                dr["Name"] = "Test" + i.ToString();
                dt.Rows.Add(dr);
            }

            ArrayList arrayList = new ArrayList();
            //create arraylsit from DataTable
            foreach (DataRow dr in dt.Rows)
            {
                arrayList.Add(dr["Name"]);
            }

            //read ArrayList items
            foreach (var item in arrayList)
            {
                Response.Write(item + "<br />");
            }
  Permalink  
Comments
Goenitz 17-Jan-14 2:36am
   
vb.net sir :(
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Here is an example to convert DataTable into ArrayList.

public ArrayList ConvertDataTableToArrayList()
 {
    DataTable dtTable = GetDataTable();
    ArrayList myArrayList = new ArrayList();
    for (int i = 0; i <= dtTable.Rows.Count - 1; i++)
    {
      for (int j = 0; j <= dtTable.Columns.Count - 1; j++)
      {
           myArrayList.Add(dtTable.Rows[i][j].ToString());
      }
    }
    return myArrayList;
 }


Hope it helps you.
  Permalink  
Comments
Goenitz 17-Jan-14 3:20am
   
Ive translated it to VB.Net but the value of dtTable.Rows[i][j].ToString is "System.Data.DataRow" how should i fix it?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Try

C#
ArrayList list=new ArrayList();
foreach (DataRow dr in dt.Rows) // dt- DataTable
{
  list.Add(dr);
}


VB
Dim list As New ArrayList()
For Each dr As DataRow In dt.Rows
list.Add(dr)
Next
  Permalink  
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161208.2 | Last Updated 17 Jan 2014
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100