Show / Hide Table of Contents

    IDataStore.Insert<TModel>(int index, TModel item) Method

    .NET Standard 2.x

    Inserts a TModel object as a data row at the position (specified by index) in the primary buffer of the DataStore. The index is the same as the zero-based row number in the primary buffer.

    Namespace: DWNet.Data

    Assembly: DWNet.Data.dll

    Syntax

      public void Insert(int index, TModel item);
    

    Parameters

    index System.Int32

    The zero-based index position for the insertion.

    item TModel

    The TModel object to be inserted to the primary buffer. The type of the model must match with the current DataObject.

    Examples

    The following code example demonstrates how to insert a data record before the position specified by an index.

    using Appeon.ApiDoc.Models;
    using DWNet.Data;
    using System;
    
    namespace Appeon.ApiDoc.IDataStoreExamples
    {
        public class InsertExample
        {
            private readonly SchoolContext _context;
    
            public InsertExample(SchoolContext dataContext)
            {
                // Sets the data context
                _context = dataContext;
            }
    
            public void Example()
            {
                // Instantiates a DataStore object with datawindow: d_department.
                var datastore = new DataStore("d_department", _context);
    
                datastore.Retrieve();
    
                // Creates a new department record
                var department =
                    new D_Department()
                    {
                        Departmentid = 10,
                        Name = "New Department",
                        Budget = 100m
                    };
    
                Console.WriteLine("Before Insert, Rowcount: {0}", datastore.RowCount);
    			
    			// Inserts the new department record before the first row.
                datastore.Insert(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: New Department; 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

    Back to top Generated by Appeon