Creating a DataRow

A DataRow is created by calling the NewRow( ) method of a DataTable, a method that takes no arguments. The DataTable supplies the schema, and the new DataRow is created with default or empty values for the fields:

// create a table with one column

DataTable dt = new DataTable();

dt.Columns.Add("MyColumn",  typeof(System.String));



// create a row with the same schema as DataTable dt

DataRow row = dt.NewRow();

row["MyColumn"] = "Item 1";



// add the row to the table

dt.Rows.Add(row);

Get ADO.NET in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.