SfGrid - Crud Action
Hi,
I'm trying to understand CRUD operations on SfGrid.
Staring with: Add
My SQL insert statement:
public Task AddUsers(User user)
{
string sql = "INSERT INTO dbo.Users (FirstName, LastName, Username, Password, UserType) VALUES (@FirstName, @LastName, @Username, @Password, @UserType);";
return _dataAccess.AddDataAsync(sql, user);
}
My SfGrid:
<SfGrid DataSource="@users" AllowPaging="true" AllowSorting="true" Toolbar="@Tool" allow ContextMenuItems="@contextMenuItems">
<GridEvents OnActionBegin="OnBeginHandler" TValue="User"/>
<GridPageSettings PageSize="10"/>
<SfDataManager>
</SfDataManager>
<GridEditSettings AllowEditing="true" AllowAdding="true" AllowDeleting="true"/>
<GridColumns>
<GridColumn Field="@nameof(User.Id)"
HeaderText="User ID"
IsIdentity="true"
IsPrimaryKey="true"
AllowEditing="false">
</GridColumn>
<GridColumn Field="@nameof(User.FirstName)"
HeaderText="First name">
</GridColumn>
<GridColumn Field="@nameof(User.LastName)"
HeaderText="Last name">
</GridColumn>
<GridColumn Field="@nameof(User.Username)"
HeaderText="Username">
</GridColumn>
<GridColumn Field="@nameof(User.Password)"
HeaderText="Password">
</GridColumn>
<GridColumn Field="@nameof(User.UserType)"
HeaderText="User type">
</GridColumn>
</GridColumns>
</SfGrid>
@code {
private List<User> users;
protected override async Task OnInitializedAsync()
{
users = await _userData.GetAllUsers();
}
private void OnBeginHandler(ActionEventArgs<User> Args)
{
if (Args.RequestType == Syncfusion.Blazor.Grids.Action.Save)
{
if (Args.Action == "Add")
{
var newUser = Args.Data;
// Call procedure to insert into DB
_userData.AddUsers(newUser);
}
}
}
}
While the code above doesn't generate an error, the data is never inserted into the SQL database...
Please help...
SIGN IN To post a reply.
1 Reply
1 reply marked as answer
RN
Rahul Narayanasamy
Syncfusion Team
October 26, 2020 02:32 PM UTC
Hi Preveen,
Query: SfGrid - Crud Action
We have validated your query and we might suspect that you have faced the problem while performing CRUD operation. Here, we have prepared a sample for your reference.
(Or)
If you want to bind the data from SQL server to DataGrid. You can consume data from SQL Server using Microsoft SqlClient and bound it to Blazor DataGrid. You can achieve this requirement by using Custom Adaptor.
We have already documented this topic in our documentation. Find the below link for your reference.
Reference:
https://blazor.syncfusion.com/documentation/datagrid/data-binding/#sql-server-data-bindingsql-client
For performing data manipulation, you can override available methods such as Insert/InsertAsync, Update/UpdateAsync and Remove/RemoveAsync of the Custom Adaptor.
Please let us know if you have any concerns.
Regards,
Rahul
Marked as answer
SIGN IN To post a reply.