Creating filling and appending a record

workTab1e.Rows.Add(new Object() {1, "Smith})


illegal for non-trainer use

Introduction Creating a new record

Filling the new record

Appending the record to a DataTable

Creating, filling, and appending a record

After you create a DataTable and define its structure by using columns and constraints, you can add new rows of data to the table.

To add a new row to a DataTable, you declare a new variable of the type DataRow. A new DataRow object is returned when you call the NewRow method. The DataTable then creates the DataRow object based on the structure of the table, as defined by the DataColumnCollection.

Dim drNewEmployee As DataRow = dtEmp1oyees.NewRow()

After adding a new row to a DataTable, you can manipulate the new row by using an index or the column name.

drNewEmployee(O) = 11 drNewEmployee(l) = "Smith"

drNewEmp1oyee("Emp1oyeeID") = 11 drNewEmp1oyee("LastName") = "Smith"

After data is inserted into the new row, the Add method is used to add the row to the DataRowCollection.

dtEmp1oyees.Rows.Add(d rNewEmp1oyee)

You can also call the Add method to add a new row by passing in an array of values, typed as Object.

dtEmp1oyees.Rows.Add(New Object() {11, "Smith"})

This technique creates a new row inside the table, and sets its column values to the values in the object array. Note that values in the array are matched sequentially to the columns, based on the order in which they appear in the table.

0 0

Post a comment