Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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 Smile | :)
Thanks
Posted 16-Jan-14 20:15pm
Goenitz558
Comments
thatraja at 17-Jan-14 2:19am
   
Don't have Google? how about bing? or other search engines?
Goenitz at 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 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 + "
");
            }
  Permalink  
Comments
Goenitz at 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 at 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
0 Sergey Alexandrovich Kryukov 540
1 OriginalGriff 290
2 CPallini 240
3 BillWoodruff 155
4 ChauhanAjay 149
0 OriginalGriff 6,609
1 Sergey Alexandrovich Kryukov 6,240
2 CPallini 5,180
3 George Jonsson 3,574
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 17 Jan 2014
Copyright © CodeProject, 1999-2014
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