|
<SfGrid @ref="Grid" DataSource="@Orders" AllowPaging="true" AllowSorting="true" Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update"})">
<GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true"></GridEditSettings>
<GridEvents OnActionBegin="ActionBeginHandler" TValue="Order"></GridEvents>
<GridPageSettings PageSize="5"></GridPageSettings>
<GridColumns>
<GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn>
. . .
</GridColumns>
</SfGrid>
@code{
public IQueryable<Order> Orders { get; set; }
public IQueryable<Order> NewOrders { get; set; }
SfGrid<Order> Grid;
OrderDataAccessLayer db = new OrderDataAccessLayer();
protected override void OnInitialized()
{
Orders = db.GetAllOrders().AsQueryable();
}
public void ActionBeginHandler(ActionEventArgs<Order> Args)
{
if(Args.RequestType == Syncfusion.Blazor.Grids.Action.Save && Args.Action == "Add")
{
var IsPresent = Orders.Where(or => or.OrderID == Args.Data.OrderID).FirstOrDefault();
if (IsPresent != null)
{
Args.Cancel = true; //if the same order id field exist then here we have cancelled default saving action. It will stay in edit mode
} else
{
db.AddOrder(Args.Data); // insert the record into your datasource
}
}
if (Args.RequestType == Syncfusion.Blazor.Grids.Action.Delete)
{
var id = Args.Data.OrderID;
db.DeleteOrder(id); // delete the record }
if (Args.RequestType == Syncfusion.Blazor.Grids.Action.Save && Args.Action == "edit")
{
db.UpdateOrder(Args.Data as Order); // update the record into your datasource
}
}
}
|
I need to handle duplicate record in "Batch Mode".Please share the example.
Note:-
I need to show "Messagebox" when adding duplicate row.
|
<SfGrid 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 OnCellSave="CellSaveHandler" 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>
<SfDialog Width="250px" IsModal="true" @bind-Visible="@IsVisible">
<DialogEvents OnOverlayClick="@OnOverlayclick">
</DialogEvents>
<DialogTemplates>
<Content> The entered id is already present. Please enter new unique id </Content>
</DialogTemplates>
</SfDialog>
@code{
public List<Order> Orders { get; set; }
. . .
public void CellSaveHandler(CellSaveArgs<Order> args)
{
if(args.ColumnName == "OrderID")
{
var IsPresent = Orders.Where(or => or.OrderID == (int?)args.Value).FirstOrDefault();
if (IsPresent != null)
{
args.Cancel = true;
OpenDialog();
} else
{
// insert the record into your datasource
}
}
}
private bool IsVisible { get; set; } = false;
private void OpenDialog()
{
this.IsVisible = true;
}
private void OnOverlayclick(MouseEventArgs arg)
{
this.IsVisible = false;
}
} |
sorry for late reply..
I have another issue..
When I add newly row for duplicate record checking, then its mot working.
please check my comments in below screencast..
https://www.screencast.com/t/tTcHdhPxsi
|
<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 OnCellSave="CellSaveHandler" TValue="Order"></GridEvents>
<GridColumns>
. ..
</GridColumns>
</SfGrid>
<SfDialog Width="250px" IsModal="true" @bind-Visible="@IsVisible">
. ..
</SfDialog>
@code{
SfGrid<Order> Grid;
public List<Order> Orders { get; set; }
. . .
public async Task CellSaveHandler(CellSaveArgs<Order> args)
{
var changes = await Grid.GetBatchChangesAsync();
if(args.ColumnName == "OrderID")
{
var IsPresent = Orders.Where(or => or.OrderID == (int?)args.Value).FirstOrDefault();
if (IsPresent != null)
{
args.Cancel = true;
OpenDialog();
} else if (changes.AddedRecords.Count > 1)
{
var added = changes.AddedRecords.Select(x => x.OrderID).ToList();
added.RemoveAt(0);
if (added.Contains((int?)args.Value))
{
args.Cancel = true;
OpenDialog();
}
}
else
{
// insert the record into your datasource
}
}
}
} |
Thanks for reply..
above code working fine but when I press "Escape" key, then its not working (its accepting duplicate value).please check this and advise how to resolve this issu..