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!