IDataStore.InsertRow<TModel>(int row, TModel model) Method
.NET Standard 2.x
Generic method. Inserts a TModel object as a row to the specified position in the primary buffer of the DataStore.
Namespace: DWNet.Data
Assembly: DWNet.Data.dll
Syntax
public int InsertRow<TModel>(int row, TModel model);
Type Parameters
TModel
The type of the model matches with the current DataObject.
Parameters
row System.Int32
The zero-based row number for the insertion.
model TModel
The model instance to be inserted to the primary buffer.
Returns
System.Int32
Returns the zero-based number of the row that was added if it succeeds.
Remarks
The status flag for the newly inserted row is ModelState.NewModified, and the row is included in the modified count.
InserRow has the same function as Insert, but InserRow has better performance because it supports generic type.
Examples
The following code example demonstrates how to insert a row of data before the position specified by a row number.
using Appeon.ApiDoc.Models;
using DWNet.Data;
using System;
namespace Appeon.ApiDoc.IDataStoreExamples
{
public class InsertRowExample
{
private readonly SchoolContext _context;
public InsertRowExample(SchoolContext dataContext)
{
// Sets the data context
_context = dataContext;
}
public void Example2()
{
// Instantiates a DataStore object with datawindow: d_department.
var datastore = new DataStore("d_department", _context);
datastore.Retrieve();
// Gets the department object of the first row.
var department = datastore.GetModel<D_Department>(0);
department.Departmentid = 10;
department.Name = "Department Name";
department.Budget = 100m;
Console.WriteLine("Before Insert, Rowcount: {0}", datastore.RowCount);
// Inserts the new department record as a row before the first row.
datastore.InsertRow(0, department);
Console.WriteLine("After Insert, Rowcount: {0}", datastore.RowCount);
for (int row = 0, rowcount = datastore.RowCount; row < rowcount; row++)
{
Console.WriteLine("DepartmentID: {0}; Name: {1}; Budget: {2}",
datastore.GetItem<int>(row, "departmentid"),
datastore.GetItem<string>(row, "name"),
datastore.GetItem<decimal>(row, "budget"));
}
/*This code produces the following output:
Before Insert, Rowcount: 4
After Insert, Rowcount: 5
DepartmentID: 10; Name: Department Name; Budget: 100
DepartmentID: 1; Name: Engineering; Budget: 350000.0000
DepartmentID: 2; Name: English; Budget: 120000.0000
DepartmentID: 4; Name: Economics; Budget: 200000.0000
DepartmentID: 7; Name: Mathematics; Budget: 250000.0000
*/
}
}
}
Example Refer To
Model Class: D_Department
DataWindow File: d_department
Applies to
.NET Standard
2.x