OnActionComplete calls a handler with args.Data = null
|
<EjsGrid TValue="Order" DataSource="@GridData" @ref="Grid" AllowPaging="true" Toolbar="@(new List<Object>() { "Add", "Edit", "Delete", "Cancel", "Update", new ItemModel() { Text = "Save", TooltipText = "Save", PrefixIcon = "M_PV_Save", Id = "DirtySave", Align=ItemAlign.Right} })">
<GridEvents OnToolbarClick="ToolbarClickHandler" OnActionBegin="OnBegin" TValue="Order" />
. . .. . . . .
</EjsGrid>
@code{
EjsGrid<Order> Grid;
. . . .. . . . . . .. .. . .
public void OnBegin(ActionEventArgs<Order> Args)
{
if (Args.RequestType == Syncfusion.EJ2.Blazor.Grids.Action.Save && Args.Action == "add")
{
if (Args.Data.OrderID == null)
{
Args.Cancel = true; //Prevent the default action
Db.AddOrder(Args.Data); // insert the record into your datasource
Db.SaveChanges(); // save the changes in your databased
Grid.Refresh(); // and refresh the Grid.
}
}
}
,. . . . .. . . . ..
}
|
|
<EjsGrid TValue="Order" DataSource="@GridData" @ref="Grid" AllowPaging="true" Toolbar="@(new List<Object>() { "Add", "Edit", "Delete", "Cancel", "Update", new ItemModel() { Text = "Save", TooltipText = "Save", PrefixIcon = "M_PV_Save", Id = "DirtySave", Align=ItemAlign.Right} })">
<GridEvents OnToolbarClick="ToolbarClickHandler" OnActionBegin="OnBegin" TValue="Order" />
. . .. . . . .
</EjsGrid>
@code{
EjsGrid<Order> Grid;
protected override void OnInitialized()
{
GridData = OrderData.GetAllOrders().ToList();
}
. . . .. . . . . . .. .. . .
public void OnBegin(ActionEventArgs<Order> Args)
{
if (Args.RequestType == Syncfusion.EJ2.Blazor.Grids.Action.Save && Args.Action == "add")
{
if (Args.Data.OrderID == null)
{
Args.Cancel = true; //Prevent the default action
Db.AddOrder(Args.Data); // insert the record into your datasource
Db.SaveChanges(); // save the changes in your databased
//update the Grid datasource with updated database records
GridData = OrderData.GetAllOrders().ToList();
}
}
}
,. . . . .. . . . ..
}
|
<EjsGrid TValue="TblClientUsers" @ref="DefaultGrid" DataSource="@colClientUsers"
Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" })" AllowSorting="true" AllowMultiSorting="true" AllowResizing="true" AllowFiltering="false" AllowExcelExport="false" ShowColumnChooser="false" GridLines="GridLine.Both"
EnableVirtualization="false" AllowPaging="true" Height="410" Width="auto">
<GridEvents OnActionBegin="ActionBeginHandler" OnActionComplete="ActionCompletedHandler" OnActionFailure="ActionFailureHandler" TValue="TblClientUsers"></GridEvents>
<GridEditSettings AllowAdding="true" AllowDeleting="true" AllowEditing="true"></GridEditSettings>
<GridColumns>
<GridColumn Field=@nameof(TblClientUsers.ClientNumber) HeaderText=@txt("txt" + "ClientNumber") IsPrimaryKey="true" TextAlign="TextAlign.Left" AllowEditing="false" Width=120></GridColumn>
<GridColumn Field=@nameof(TblClientUsers.ClientName) HeaderText=@txt("txt" + "ClientName") TextAlign="TextAlign.Left" EditType="EditType.DefaultEdit" Width="120"></GridColumn>
......
@code{
private
EjsGrid<TblClientUsers> DefaultGrid;
List<TblClientUsers> colClientUsers;
protected override async Task OnInitializedAsync()
{
try
{
await Task.Delay(100);
colClientUsers = await
@Db.GetAllTblClientUsers(SQLDbTblLogin1);
}
catch (Exception ex)
{ throw (ex); }
}
public async void ActionBeginHandler(ActionEventArgs<TblClientUsers> Args)
{
if (Args.RequestType == Syncfusion.EJ2.Blazor.Grids.Action.Save
&& Args.Action == "add")
{
Args.Cancel = true; //Prevent
the default action
if (!validateClientData(Args.Data))
return;
else if
(Db.AddTblClientUsers(Args.Data))
{
Db.SaveChanges();
}
else
{
ToastShow("Cannot save the client data");
}
colClientUsers = await
@Db.GetAllTblClientUsers(SQLDbTblLogin1);
await Task.Delay(100);
DefaultGrid.Refresh();
}
|
<EjsGrid @ref="Grid" DataSource="@GridData" Toolbar="@(new List<string> { "Add", "Edit", "Delete", "Cancel", "Update" })" AllowFiltering="true" AllowSorting="true" AllowPaging="true">
<GridEditSettings AllowAdding="true" AllowDeleting="true" AllowEditing="true"></GridEditSettings>
<GridEvents OnActionBegin="OnBegin" OnActionComplete="OnComplete" TValue="Order"></GridEvents>
<GridColumns>
<GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsIdentity="true" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn>
<GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="150"></GridColumn>
<GridColumn Field=@nameof(Order.EmployeeID) HeaderText="Id" Width="150"></GridColumn>
</GridColumns>
</EjsGrid>
@code{
EjsGrid<Order> Grid { get; set; }
public List<Order> GridData = new List<Order>();
public Order AddedData = new Order();
public Order EditedData = new Order();
protected override void OnInitialized()
{
GridData = OrderData.GetAllOrders().ToList();
}
public async void OnComplete(ActionEventArgs<Order> Args)
{
if (Args.RequestType == Syncfusion.EJ2.Blazor.Grids.Action.Save)
{
if (AddedData.OrderID != null)
{
//if your primarykey column is autoincremented, then remove the value of Primarykey while inseting into your database
AddedData.OrderID = null;
// insert new record in your database
await Task.Run(() => OrderData.AddOrder(AddedData));
AddedData = new Order();
}
else
{
//update the changes in your database
await Task.Run(() => OrderData.UpdateOrder(EditedData));
EditedData = new Order();
}
}
else if (Args.RequestType == Syncfusion.EJ2.Blazor.Grids.Action.Delete)
{
await Task.Run(() => OrderData.DeleteOrder(Args.Data.OrderID)); // delete the record from your database
}
}
public async void OnBegin(ActionEventArgs<Order> Args)
{
if (Args.RequestType == Syncfusion.EJ2.Blazor.Grids.Action.Save)
{
if (Args.Action == "add")
{
AddedData = Args.Data; // take the inserted record details
}
else
{
EditedData = Args.Data; //store the edited record details
}
// if your primarykey is auto increment column then its value will be null
if (Args.Data.OrderID == null)
{
//so kindly define the incremented value to primarykey to update in the Grid
Args.Data.OrderID = OrderData.GetAllOrders().ToList().Max(x => x.OrderID) + 1; // so assign value to IsIdentity
}
}
}
}
|
- 7 Replies
- 2 Participants
-
AN Andrzej
- Mar 9, 2020 10:19 PM UTC
- Mar 13, 2020 04:38 AM UTC