Putting a List into a datatable column in c# -
i trying display values 2 different column in 1 datatable
column. possible add them list
, put list
datatable
column?
datatable dt = new datatable(); dt.columns.add("fullname"); dt.columns.add("studentid"); foreach (var studentsubject in studentsubjects) { dt.columns.add(studentsubject.tostring()); } foreach (var g in groups) { datarow dr = dt.newrow(); dr["fullname"] = g.fullname; dr["studentid"] = g.studentid; foreach (var sc in g.examscore) { list<subjectproperty> listsubjectproperty = new list<subjectproperty>(); listsubjectproperty.add(new subjectproperty { subjectid = sc.subjectid, score = sc.examscore }); dr[sc.subjectname] = listsubjectproperty; } dt.rows.add(dr);
try this
using system.linq; using system.text; using system.data; namespace consoleapplication1 { class program { static void main(string[] args) { datatable dt = new datatable(); dt.columns.add("a list object", typeof(list<string>)); list<string> names = null; names = new list<string>(new string[] {"john", "mary"}); dt.rows.add(names); names = new list<string>(new string[] { "bob", "carol", "ted", "alice" }); dt.rows.add(names); names = new list<string>(new string[] { "harry", "sally"}); dt.rows.add(names); } } }
Comments
Post a Comment