HOWTO: Create (Instantiate) a new DataTable row
Posted: 6/25/2009 12:17:21 PM
By: Comfortably Anonymous
Times Read: 1,941
0 Dislikes: 0
Topic: Programming: .NET Framework

While you'd think something like the following code would work, it doesn't and fails with a frustrating "No constructor defined" error:

Middleware.ProjectName.DAL.DataModel.SomeDataRow rowNew = new MiddleWare.ProjectName.DAL.DataModel.SomeDataRow();

The way to obtain an instantiated row object that belongs to a TableAdapter is as follows:

// First, instantiate a DataTable of the type the new row will belong to:
Middleware.ProjectName.DAL.DataModel.SomeDataTable dtSomeData = new Middleware.ProjectName.DAL.DataModel.SomeDataTable();

// Next, use the DataTable instance to create a new row that is the same "shape" as the DataTable:
Middleware.ProjectName.DAL.DataModel.SomeDataRow rowNew = dtSomeData.NewSomeDataRow();

It's easy as that, intuitive in retrospect, but a mind-twist to get the concept down the first time.
Anyone know a better way?

Rating: (You must be logged in to vote)