How to convert DataTable to object type List in C#

2019-01-26 02:50发布

问题:

I want to convert DataTable to List<object> in C#. Here is my 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;
}

This is the Error--

回答1:

If you have any Typed DataType for list object something like this

public class MyObj
{
    public string Name { get; set; }
    public int ID { get; set; }
}

then you can do like this

private List<MyObj> test(DataTable dt)
{

    var convertedList = (from rw in dt.AsEnumerable()
        select new MyObj() 
        {
            ID = Convert.ToInt32(rw["ID"]),
            Name = Convert.ToString(rw["Name"])
        }).ToList();

    return convertedList;
}

or if you still want List<object> then do like this

private List<object> GetListByDataTable(DataTable dt)
{

    var reult = (from rw in dt.AsEnumerable()
        select new
        {
            Name = Convert.ToString(rw["Name"]),
            ID = Convert.ToInt32(rw["ID"])
        }).ToList();

    return reult.ConvertAll<object>(o => (object)o);
}


回答2:

I hope what you are trying to do is insert the objects in the data table (which probably are returned from a query) in to a List.

public List<object> ShowMessage2(DataTable dtInput)
    {
        List<object> objectList = new List<object>();

        foreach(DataRow dr in dtInput.Rows)
        {
            MyObj newObj = new MyObj();
            newObj.ID = Convert.ToInt32(dr["ID"]);  // Beware of the possible conversion errors due to type mismatches
            newObj.Name = dr["Name"].ToString();

            objectList.Add(newObj);
        }
        return objectList;
    }

public class MyObj
{
    public int ID { get; set; }
    public string Name { get; set; }        
}

Just saw your addition to the question! I can't understand why you try to generate a list if your intention is just to display the data in a gird view, which you can do in a single line!!!

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");

dataGridView1.DataSource = dt; // THIS IS ALL U NEED! Just bind the DataTable to the grid as data source!


回答3:

The DataRow need to be created before adding it to the DataRow collection.

For instance,

DataTable dt = new DataTable();
        dt.Columns.Add("ID");
        dt.Columns.Add("Name");
        var dr = dt.NewRow();
        dr[0] = "1";
        dr[1] = "AAA";
        dt.Rows.Add(dr);
        ... likewise

        foreach (DataRow dr in dt.Rows)
        {
            obj.Add(dr);
        }

Now the List should hold the Rows. Let me know if it works.