In a DataTable:
I can access all of the DataRow elements like so:
DataTable table = GetMyTable();
for (int i = 0; i < table.Rows.Count; i++) {
DataRow row = table.Rows[i];
Console.WriteLine(row);
}
Also, I can access all of the DataColumn elements like this:
DataTable table = GetMyTable();
for (int i = 0; i < table.Columns.Count; i++) {
DataColumn column = table.Columns[i];
Console.WriteLine(column);
}
Finally, I can access each individual Cell of the DataTable object like this:
DataTable table = GetMyTable();
for (int i = 0; i < table.Rows.Count; i++) {
DataRow row = table.Rows[i];
object[] array = row.ItemArray;
for (int j = 0; j < array.Length; j++) {
object cell = array[j];
Console.WriteLine(cell);
}
}
At least it looks like some information is lost in the third technique.
Here’s my question:
How is this array of objects being managed?
I’ve been playing with how to recreate something with this basic type of functionality (since I am already familiar with it), but I just don’t know how to design all these little sub-classes to make it work!
The table has a list of
DataColumn. This is a list of column definitions. The table has a list of rows. Each row has one cell for each column in the list of columns.The trick is that the rows are created by the table. The table knows the length of the
Columnscollection, as well as the data types of the columns and any constraints on them. When the table creates aDataRow, it creates it with the correct number of columns.