|
<SfGrid @ref="Grid" DataSource="@Orders" AllowPaging="true" Toolbar="@(new List<string>() { "Add", "Delete", "Update", "Cancel" })" Height="315">
<GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="EditMode.Batch"></GridEditSettings>
<GridEvents CellSaved="CellSavedHandler" TValue="Order"></GridEvents>
<GridColumns>
<GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" ValidationRules="@(new ValidationRules { Required = true })" Type="ColumnType.Number" Width="120"></GridColumn>
. . .
<GridColumn Field=@nameof(Order.Amount) HeaderText="Amount" Format="C2" AllowEditing="false" TextAlign="TextAlign.Right" EditType="EditType.NumericEdit" Width="120">
<Template>
@{
var value = (context as Order);
value.Amount = value.Quantity * value.UnitPrice;
<span>@value.Amount</span> //here we have calculated the values and shown at initial render
}
</Template>
</GridColumn>
<GridColumn Field=@nameof(Order.Sum) HeaderText="Sum" Format="C2" AllowEditing="false" TextAlign="TextAlign.Right" EditType="EditType.NumericEdit" Width="120">
<Template>
@{
var value = (context as Order);
value.Sum = value.Quantity + value.UnitPrice;
<span>@value.Sum</span> //here we have calculated the values and shown at initial render
}
</Template>
</GridColumn>
</GridColumns>
</SfGrid>
@code{
SfGrid<Order> Grid;
. . .
public async Task CellSavedHandler(CellSaveArgs<Order> args)
{
var index = await Grid.GetRowIndexByPrimaryKey(args.RowData.OrderID);
if (args.ColumnName == "Quantity")
{
await Grid.UpdateCell(index, "Amount", Convert.ToInt32(args.Value) * args.RowData.UnitPrice);
await Grid.UpdateCell(index, "Sum", Convert.ToInt32(args.Value) + args.RowData.UnitPrice); //here, we have calculated the values and shown in particular cell in while editing
}
else if (args.ColumnName == "UnitPrice")
{
await Grid.UpdateCell(index, "Amount", Convert.ToDouble(args.Value) * args.RowData.Quantity);
await Grid.UpdateCell(index, "Sum", Convert.ToDouble(args.Value) + args.RowData.Quantity); //here, we have calculated the values and shown in particular cell in while editing
}
}
. . .
} |
|
<SfCheckBox @bind-Checked="isChecked" Label="Change" @onchange="onChange"></SfCheckBox>
<SfGrid @ref="Grid" DataSource="@Orders" AllowPaging="true" Toolbar="@(new List<string>() { "Add", "Delete", "Update", "Cancel" })" Height="315">
<GridColumns>
. . .
<GridColumn Field=@nameof(Order.VAT) HeaderText="VAT" Format="C2" AllowEditing="false" TextAlign="TextAlign.Right" EditType="EditType.NumericEdit" Width="120"></GridColumn>
<GridColumn Field=@nameof(Order.ShipCountry) HeaderText="Ship Country" EditType="EditType.DropDownEdit" Width="150"></GridColumn>
</GridColumns>
</SfGrid>
@code{
SfGrid<Order> Grid;
. . .
private void onChange(Microsoft.AspNetCore.Components.ChangeEventArgs args)
{
if( Convert.ToBoolean(args.Value) == true)
{
foreach(var item in Orders)
{
item.VAT = 12;
}
} else
{
foreach (var item in Orders)
{
item.VAT = 0;
}
}
Grid.Refresh();
}
} |
Is there is an another way to get Index (rowindex) instead of Grid.GetRowIndexByPrimaryKey?. Grid.GetRowIndexByPrimaryKey is not applicable in newly added lineitems. since they retun 0 ID in batchmode.
|
<SfGrid @ref="Grid" DataSource="@Orders" AllowPaging="true" Toolbar="@(new List<string>() { "Add", "Delete", "Update", "Cancel" })" Height="315">
<GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="EditMode.Batch"></GridEditSettings>
<GridEvents CellSaved="CellSavedHandler" OnBatchAdd="BatchAddHandler" OnBatchSave="BatchSaveHandler" TValue="Order"></GridEvents>
<GridColumns>
<GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" ValidationRules="@(new ValidationRules { Required = true })" Type="ColumnType.Number" Width="120"></GridColumn>
. . .
</GridColumns>
</SfGrid>
@code{
SfGrid<Order> Grid;
bool IsAdd { get; set; }
. . .
public async Task CellSavedHandler(CellSaveArgs<Order> args)
{
var index = await Grid.GetRowIndexByPrimaryKey(args.RowData.OrderID);
if (args.ColumnName == "Quantity")
{
if (IsAdd)
{
args.RowData.Quantity = (int?)args.Value;
await Grid.UpdateCell(index, "Amount", Convert.ToInt32(args.Value) * 1);
await Grid.UpdateCell(index, "Sum", Convert.ToInt32(args.Value) + 0);
}
await Grid.UpdateCell(index, "Amount", Convert.ToInt32(args.Value) * args.RowData.UnitPrice);
await Grid.UpdateCell(index, "Sum", Convert.ToInt32(args.Value) + args.RowData.UnitPrice);
}
else if (args.ColumnName == "UnitPrice")
{
if (IsAdd)
{
args.RowData.UnitPrice = (double?)args.Value;
await Grid.UpdateCell(index, "Amount", Convert.ToDouble(args.Value) * 1);
await Grid.UpdateCell(index, "Sum", Convert.ToDouble(args.Value) + 0);
}
await Grid.UpdateCell(index, "Amount", Convert.ToDouble(args.Value) * args.RowData.Quantity);
await Grid.UpdateCell(index, "Sum", Convert.ToDouble(args.Value) + args.RowData.Quantity);
}
}
public void BatchAddHandler(BeforeBatchAddArgs<Order> args)
{
IsAdd = true;
}
public void BatchSaveHandler(BeforeBatchSaveArgs<Order> args)
{
IsAdd = false;
}
} |