Click here to Skip to main content
12,395,388 members (70,740 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
I want to convert DataTable to object type List in C#. for the i have written the following code. But it is not working. Please help me

public List<object> ShowMessage()
   {
       List<object> obj = new List<object>();
 
       DataTable dt = new DataTable();
       dt.Columns.Add("ID");
       dt.Columns.Add("Name");
 
       dt.Rows.Add("1","AAA");
       dt.Rows.Add("2", "BBB");
       dt.Rows.Add("3", "CCC");
 
       foreach (DataRow dr in dt.Rows)
       {
 
           obj.Add(dr);
       }
 
       return obj;
   }


NB:Just i want to convert a datatable to a list.You may not consider my above code.Just give me the code to convert a DataTable to a List. I am using .Net Framework 4.0


Thanks in advance
Rashed
Posted 10-Oct-12 21:07pm
Updated 10-Oct-12 21:46pm
v2
Comments
n.podbielski 11-Oct-12 3:11am
   
And how exactly it is not working? Can you paste some errors? What is desired behavior?
   
List<object> is in fact untyped list (as everything is object), normally, totally pointless.
In your sample, it should be List<DataRow>. (But why?!)
Just a note.
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Can you try something like this:
List<object> lst = dt.AsEnumerable().ToList<object>();


--Amit
  Permalink  
v2
Comments
_Amy 11-Oct-12 4:32am
   
Check the link below:
http://s9.postimage.org/ts5ca0sq7/Untitled.png[^]
Here I used same code.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

This is how you should correctly add a data row:
http://msdn.microsoft.com/en-us/library/system.data.datatable.rows.aspx[^].

Besides, your code looks totally pointless. And hard coded immediate constants like "ID", "Name" is evil. This style makes code unsupportable. However, I would understand if you are just experimenting.

—SA
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

hi
try this

public static List<Object> ConvertRowsToList<Object>( DataTable input, Convert<DataRow, Object> conversion) {
    List<Object> Objval = new List<Object>()
    foreach(DataRow dr in input.Rows)
        Objval.Add( conversion(dr) );
 
    return Objval;
}
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

public static IList<object> ConvertTo<object>(DataTable table)
    {
        if (table == null)
            return null;
 
        List<DataRow> rows = new List<DataRow>();
 
        foreach (DataRow row in table.Rows)
            rows.Add(row);
 
        return ConvertTo<object>(rows);
    }
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

googly search..
googly search..
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 8

  Permalink  

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.160721.1 | Last Updated 22 May 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