The DataRow/Table Connection

Published 10.25.2007 by ~mattg

Having used a custom data layer for the past two years, I haven’t had to work too much with ADO.NET. Where I’m at now it’s used a bit more frequently, so I’ve gotten the chance to explore it’s idiosyncrasies a bit more. One interesting thing I noted was the connection a DataRow maintains with it’s DataTable when the DataRow is created via the table’s NewRow() function.

DataTable dt = new DataTable();

DataColumn c = new DataColumn();
c.ColumnName = “Name”;
dt.Columns.Add(c);

DataRow row = dt.NewRow();
row[“Name”] = “My Name”;

DataColumn desc = new DataColumn();
desc.ColumnName = “Description”;
dt.Columns.Add(desc);

row[“Desc”] = “My Description”;

My first thought is that the above wouldn’t work because row would have no idea that I added a column to it’s parent datatable. However, when you create the DataRow using .NewRow(), the link is maintained. So the above works just fine.

Filed under .NET Development

Comments (0)

Comments RSS - Trackback - Write Comment

No comments yet

Write Comment