when using a viewmodel to bind to grid, the view model MUST have a parameterless contrustrutor?
can existing viewmodel on the row edit to be used directly for edit template instead creating a new viewmodel? sometime the viewmodel has reference and complex logic so can't be fully intilized from parameter less constructor.
@page "/datagrid-features"
@using Syncfusion.Blazor.Grids
<h2>DataGrid</h2>
<br/>
<div id="ControlRegion">
<SfGrid ID="Grid" DataSource="@Orders" @ref="Grid"
AllowReordering="true" AllowResizing="true" AllowSorting="true" AllowFiltering="true"
Height="500" Width="auto" EnableVirtualization="true">
<GridFilterSettings Type="Syncfusion.Blazor.Grids.FilterType.Menu"></GridFilterSettings>
<GridPageSettings PageSize="20"></GridPageSettings>
<GridEditSettings AllowAdding="true" AllowDeleting="true" AllowEditing="true"></GridEditSettings>
<GridColumns>
<GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn>
<GridColumn HeaderText="Sub items">
<Template>
@{
var vm = context as Order;
for (int i = 1; i <= vm.LineitemSize; i++)
{
<div>lineitem: @i</div>
}
}
</Template>
</GridColumn>
<GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="120"></GridColumn>
<GridColumn Field=@nameof(Order.OrderDate) HeaderText=" Order Date" EditType="EditType.DatePickerEdit" Format="d" TextAlign="TextAlign.Right" Width="130" Type="ColumnType.Date"></GridColumn>
<GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120">
<EditTemplate>
@{
var vm = context as Order;
<input type="text" @bind-value="@vm.Freight" />
}
</EditTemplate>
</GridColumn>
<GridColumn Field=@nameof(Order.FreightPct) HeaderText="Freight percent" Width="150"></GridColumn>
<GridColumn Field=@nameof(Order.ShipCountry) HeaderText="Ship Country" Width="150"></GridColumn>
</GridColumns>
</SfGrid>
</div>
<br/>
<br/>
<style>
.ulstyle {
margin: 0px;
padding-left: 20px;
display: inline-block;
}
.list {
float: left;
line-height: 20px;
margin: 10px;
min-width: 200px;
}
</style>
@code{
public List<Order> Orders { get; set; }
SfGrid<Order> Grid;
protected override void OnInitialized()
{
Orders = Enumerable.Range(1, 8000).Select(x => new Order(x)
{
LineitemSize = new Random().Next(1,4),
CustomerID = (new string[] { "ALFKI", "ANANTR", "ANTON", "BLONP", "BOLID" })[new Random().Next(5)],
Freight = 2.1 * x,
OrderDate = DateTime.Now.AddDays(-x),
ShipCountry = (new string[] { "USA", "UK", "CHINA", "RUSSIA", "INDIA" })[new Random().Next(5)]
}).ToList();
}
public class Order
{
public Order(int id)
{
OrderID = id;
}
public int? OrderID { get; set; }
public int LineitemSize { get; set; }
public string CustomerID { get; set; }
public DateTime? OrderDate { get; set; }
public double? Freight { get; set; }
public string ShipCountry { get; set; }
public double? FreightPct => Freight / 100;
}
}