Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
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
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 :)

C#
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.
 
Share this answer
 
C#
//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 />");
           }
 
Share this answer
 
Comments
Goenitz 17-Jan-14 2:36am    
vb.net sir :(
Here is an example to convert DataTable into ArrayList.

C#
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.
 
Share this answer
 
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?
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
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900