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--
The DataRow need to be created before adding it to the DataRow collection.
For instance,
Now the List should hold the Rows. Let me know if it works.
If you have any Typed DataType for list object something like this
then you can do like this
or if you still want
List<object>
then do like thisI 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.
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!!!