Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a datatable cell contains a datatable?

Tags:

c#

ado.net

If not which structure should I use ?

like image 425
programmernovice Avatar asked Sep 07 '09 22:09

programmernovice


2 Answers

You can add a new DataTable to a DataTable cell. Simple Example:

Define 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 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 finally, add a row to the primary table:

people.Rows.Add("Dorothy", friends);

To get the sub table from the primary table you need to cast the object as DataTable:

DataTable output = (DataTable)people.Rows[0]["Friends"];
like image 137
Or Shabtay Avatar answered Oct 22 '22 10:10

Or Shabtay


DataSet can contain several datatables with different schemas. You can store your second datatable in a second table and reference it to your row at first table.

Also you can use WriteXml and ReadXml methods to store your datatable into a data cell.

1: Take a look at that answer. 1: What are useful JavaScript methods that extends built-in objects?

like image 3
Canavar Avatar answered Oct 22 '22 10:10

Canavar