Navigation: DbNetGridCore >

Constructors

 

 

 

 

 

DbNetGridCore is server-side ASP.NET Core grid component designed to be used in a Razor view. The grid can be configured to either directly connect to a database by specifying a connection alias and the tables/views or to use indirect API based data such as a JSON or a list of typed objects.

 

If you are connecting directly to the database the constructor has 2 mandatory properties which should be passed to the constructor which are Connection and FromPart

 

...

@(new DbNetGridCore("northwind", "customers").Render())

...

 

Demo

 

It also has 3rd and 4th optional parameters:

 

Id  Specifies the Id of an existing element on the page that want the control to be rendered inside of 

DataSourceType - defaults to TableOrView but should be set to StoredProcedure when you are connection to a stored procedure

 

Constructor when rendering in an exisiting HTML element

 

...

<table>

    <tr>

        <td class="dashboard">

            <h6>Categories</h6>

            <hr />

            <div id="categoriesGrid"></div>

            <h6>Shippers</h6>

            <hr />

            <div id="shippersGrid"></div>

            <h6>Employees</h6>

            <hr />

            <div id="employeesGrid"></div>

        </td>

        <td class="dashboard">

            <h6>Regions</h6>

            <hr />

            <div id="regionsGrid"></div>

            <h6>Territories</h6>

            <hr />

            <div id="territoriesGrid"></div>

        </td>

    </tr>

</table>

...

 

Note the 3rd parameter specifies the Id of the HTML element inside of which the grid will be rendered

 

...

DbNetGridCore categoriesGrid = new DbNetGridCore("northwind", "categories", "categoriesGrid") { ToolbarPosition = ToolbarPosition.Hidden };

categoriesGrid.Column("Picture").Image(new ImageConfiguration("jpg"));

categoriesGrid.Column("CategoryId").Hidden();

@categoriesGrid.Render()

 

DbNetGridCore regionsGrid = new DbNetGridCore("northwind", "regions", "regionsGrid") { ToolbarPosition = ToolbarPosition.Hidden };

regionsGrid.Column("RegionId").Hidden();

@regionsGrid.Render()

 

DbNetGridCore territoriesGrid = new DbNetGridCore("northwind", "territories", "territoriesGrid") { PageSize = 20, Search = false, Export = false, Copy = false };

territoriesGrid.Column("RegionID").Lookup(new Lookup("regions", "regionId", "regiondescription"));

territoriesGrid.Column("TerritoryId").Hidden();

@territoriesGrid.Render()

 

DbNetGridCore shippersGrid = new DbNetGridCore("northwind", "shippers", "shippersGrid") { ToolbarPosition = ToolbarPosition.Hidden };

shippersGrid.Column("ShipperId").Hidden();

@shippersGrid.Render()

 

DbNetGridCore employeesGrid = new DbNetGridCore("northwind", "employees", "employeesGrid") { ToolbarPosition = ToolbarPosition.Hidden };

employeesGrid.Columns = new List<string>() { "LastName", "FirstName", "Title", "HomePhone" };

employeesGrid.Labels = new List<string>() { "Surname", "Forename", "Job Title", "Phone" };

employeesGrid.Column("EmployeeId").Hidden();

@employeesGrid.Render()

...

 

Demo

 

Constructor when using a stored procedure as a data source

 

...

DbNetGridCore grid = new DbNetGridCore("northwind", "[dbo].[EmployeeSalesInLast30Days]", null, DataSourceType.StoredProcedure);

grid.ProcedureParams["Beginning_Date"] = DateTime.Today.AddDays(-30);

grid.ProcedureParams["Ending_Date"] = DateTime.Today;

@grid.Render()

...

 

Any parameters accepted by the stored procedure can be assigned using the ProcedureParams property

 

Specifying the database type in the constructor

 

If you are using a database other than MS SQL Server or SQLite which have their Data Providers included in the .Net Framework then you will need to install the required Data Provider for the database you are connecting to and to also specify the type of database in the constructor

 

...

DbNetGridCore customersGrid = new DbNetGridCore("sakila(mysql)", DatabaseType.MySQL, "customer")

{

    Update = true,

    PageSize = 10

};

@customersGrid.Render()

...

 

 

Using JSON and typed lists as data sources

 

In addition to using a database as a data source you can also use JSON or a list of typed objects (List<T>) generated by external API's

 

Consuming JSON as a data source

JSON can be consumed as a grid data source as a file by specifying a DataSourceType of JSON and the JSON file Url in the constructor. An optional third argument of the Type that represents the JSON can also be supplied which helps the grid control identify the types of each of the columns. The Url can be either for the local server or a remote server.

...

DbNetGridCore employeesGrid = new DbNetGridCore(DataSourceType.JSON, "/data/employees.json", typeof(Employee));

employeesGrid.Column(new string[] { nameof(Employee.Gender), nameof(Employee.DepartmentName), nameof(Employee.Status) }).Lookup();

employeesGrid.Columns = new List<string>() {

    nameof(Employee.EmployeeKey),

    nameof(Employee.LastName),

    nameof(Employee.FirstName),

    nameof(Employee.NameStyle),

    nameof(Employee.Title),

    nameof(Employee.Gender),

    nameof(Employee.DepartmentName),

    nameof(Employee.Status);

};

employeesGrid.Column(nameof(Employee.EmployeeKey)).PrimaryKey();

@employeesGrid.Render()

...

 

Demo

 

JSON can also be assigned to the grid as a string using the AddJson generic method specify the Type represented by the JSON

 

...

DbNetGridCore employeesGrid = new DbNetGridCore(DataSourceType.JSON);

employeesGrid.AddJson<Employee>(Model.JsonData);

employeesGrid.Column(new string[] { nameof(Employee.Gender), nameof(Employee.DepartmentName), nameof(Employee.Status)}).Lookup();

employeesGrid.Column(nameof(Employee.EmployeeKey)).PrimaryKey();

@employeesGrid.Render()

...

 

 

Consuming a list of typed objects (List<T>) as a data source

 

If you want to use a List of objects perhaps created by an API you need to specify a DataSourceType of List in the constructor and then use the AddList method to assign the list of objects to grid control.

 

...

DbNetGridCore productsGrid = new DbNetGridCore(DataSourceType.List);

productsGrid.AddList<Product>(Model.Products);

productsGrid.Column(nameof(Product.ProductID)).PrimaryKey();

productsGrid.Column(nameof(Product.UnitPrice)).Format("c");

productsGrid.Column(nameof(Product.Discontinued)).DataType(typeof(bool));

productsGrid.Column(nameof(Product.SupplierID)).Lookup(Model.SupplierLookup);

productsGrid.Column(nameof(Product.CategoryID)).Lookup(Model.CategoryLookup);

productsGrid.Column("*").Filter();

@productsGrid.Render()

...

 

 

 

 

 

 

Copyright © 2023 DbNetLink