[ACCEPTED]-Can a datatable cell contains a datatable?-ado.net
You can add a new
DataTable to a
Define 5 the primary table with one cell type
DataTable people = new DataTable(); people.Columns.Add("Name", typeof(string)); people.Columns.Add("Friends", typeof(DataTable));
Define 4 the sub table:
DataTable friends = new DataTable(); friends.Columns.Add("Name", typeof(string)); friends.Columns.Add("Desire", typeof(string));
Add some data to the sub table:
friends.Rows.Add("Scarecrow", "brain"); friends.Rows.Add("Tin Woodman", "heart"); friends.Rows.Add("Cowardly Lion", "courage");
And 3 finally, add a row to the primary table:
To 2 get the sub table from the primary table 1 you need to cast the object as
DataTable output = (DataTable)people.Rows["Friends"];
DataSet can contain several datatables with 5 different schemas. You can store your second 4 datatable in a second table and reference 3 it to your row at first table.
Assuming you mean to persist it to a database, you 4 would be better served looking at a foreign-key 3 /
DataRelation. If you really want more structured data in 2 a cell, then perhaps something like xml, or 1 some other serialization format.
According to what I infer from your question 4 you can say something like this :
DataRow dr = new DataRow(); DataTable dtable = dr.Table
MSDN : DataRow's 3 DataTable Property : Gets the DataTable 2 for which this row has a schema.
Example 1 and Explanation : http://msdn.microsoft.com/en-us/library/system.data.datarow.table.aspx
More Related questions