Hi Support,
I'm using the SfGrid with a Custom DataAdaptor that interfaces with my DbContext for CRUD activities. I have a column in my Entity that is configured as a Unique Index in my ModelBuilder. When I add a row to the grid and I don't use a unique name, I don't receive any feedback from the UI other than the entry doesn't save when I change focus. There are a couple of other items on this forum that have a similar issue but there doesn't seem to be a way to address this using the DataAdaptor.
The first attempt I made was to use an EventCallback from my DataAdaptor component, which you can see in the InsertAsync function below. That does not work. When I register a handler for this in my Grid razor component, it never gets called.
I also attempted to register the GridEvents OnActionFailure event and that does not fire when the DataAdaptor fails to insert.
My preference would be to be able to handle this in the DataAdaptor where I could return the database error to the UI if it fails that validation but I don't see how that could be done with the overloaded InsertAsync function.
I want to use the DataAdaptor for this work if possible as well as not loading my entities twice inside the razor control (one for the grid and one for the validation) to do this.
My Grid:
```
```
My Custom DataAdaptor:
```
Hi Joseph,
Based on your reported issue, we
suggest using the IsIdentity property in the primary column to avoid duplicate
keys. If the primary key column value is auto-incremented in your database,
kindly define the IsIdentity property of the GridColumn as true for the
PrimaryKey column. When IsIdentity is enabled, it will be considered a
read-only column when editing or adding a record. Refer to the provided documentation for further details.
API Link: https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.GridColumn.html#Syncfusion_Blazor_Grids_GridColumn_IsIdentity
If you don’t want the above suggestion, we recommend using the
RowUpdating event to check for duplicate data. If duplicate data is detected,
cancel the add action using args.Cancel = true. You can then display an error
message or customize the action as needed. Please refer to the code snippet and
documentation below for further reference.
|
<SfGrid @ref="@Grid" TValue="Order" ID="Grid" AllowPaging="true" Toolbar="@(new List<string>() { "Add", "Edit","Update","Delete","Cancel" })" Height="315"> <SfDataManager AdaptorInstance="@typeof(CustomAdaptor)" Adaptor="Adaptors.CustomAdaptor"></SfDataManager> <GridEditSettings AllowAdding="true" AllowDeleting="true" AllowEditing="true" Mode="EditMode.Normal"></GridEditSettings> <GridPageSettings PageSizes="true"></GridPageSettings>
<GridEvents RowUpdating="RowUpdatingHandler" TValue="Order"></GridEvents>
</SfGrid>
@code {
public void RowUpdatingHandler(RowUpdatingEventArgs<Order> args) { //if duplicates records finds using the args.Cancel property to cancel the add opertion. } |
Reference:
https://blazor.syncfusion.com/documentation/datagrid/events#rowupdating
Regards,
Prathap S