[ACCEPTED]-Can a datatable cell contains a datatable?-ado.net
You can add a new DataTable
to a DataTable
cell.
Simple Example:
Define 5 the primary table with one cell type DataTable
:
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:
people.Rows.Add("Dorothy", friends);
To 2 get the sub table from the primary table 1 you need to cast the object as DataTable
:
DataTable output = (DataTable)people.Rows[0]["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.
Also you can 2 use WriteXml and ReadXml methods to store your datatable 1 into a data cell.
1: Take a look at that answer. 1: What are useful JavaScript methods that extends built-in objects?
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[0].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
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.