I have a SfGrid which doesn't allow me to add new rows(the add button is shown as clicked, but it doesn't display the input row) if the database table is empty. No errors are thrown. If I manually insert a row in the SSMS(where my database is), the row is shown and I can add new rows normally(everything works). I'm using Syncfusion for my Blazor-WebAssembly project.
Here is the SfGrid
component in my razor page:
<SfGrid TValue="Note" Toolbar="@(new List<string>() { "Add", "Edit", "Update", "Cancel" })">
<GridEvents OnActionBegin="OnBeginHandler" CommandClicked="@CommandClickHandler" TValue="Note"></GridEvents>
<SfDataManager Url="/api/Notes" Adaptor="Adaptors.WebApiAdaptor"></SfDataManager>
<GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true"></GridEditSettings>
<GridColumns>
<GridColumn Field="@nameof(Note.NoteId)" IsPrimaryKey="true" Visible="false"></GridColumn>
<GridColumn Field="@nameof(Note.Name)" ValidationRules="@(new ValidationRules(){Required=true})"></GridColumn>
<GridColumn>
<GridCommandColumns>
<GridCommandColumn Type="CommandButtonType.Delete" ButtonOption="@(new CommandButtonOptions() { IconCss = "e-icons e-delete", CssClass = "e-flat" })"></GridCommandColumn>
</GridCommandColumns>
</GridColumn>
</GridColumns>
</SfGrid>
And the @code
part of the page:
private void OnBeginHandler(ActionEventArgs<Note> args)
{
if (args.RequestType == Syncfusion.Blazor.Grids.Action.Save)
{
if (args.Action == "add")
{
var t = args.Data;
}
}
}
public void CommandClickHandler(CommandClickEventArgs<Note> args)
{
if (args.CommandColumn.Type != CommandButtonType.Save)
return;
var lookup = args.RowData;
StateHasChanged();
}
And finally, the NotesController
:
[Route("api/[controller]")]
[ApiController]
public class NotesController : ControllerBase
{
private readonly OrganizatorContext _context;
public NotesController(OrganizatorContext context)
{
_context = context;
}
// GET: api/<NotesController>
[HttpGet]
public object Get()
{
return new { Items = _context.Notes, Count = _context.Notes.Count() };
}
// GET api/<NotesController>/5
[HttpGet("{id}")]
public object Get(long id)
{
return new { Items = _context.Notes.Where(x => x.NoteId.Equals(id)).FirstOrDefault(), Count = _context.Notes.Count() };
}
// POST api/<NotesController>
[HttpPost]
public void Post([FromBody] Note note)
{
_context.Notes.Add(note);
_context.SaveChanges();
}
// PUT api/<NotesController>/5
[HttpPut("{id}")]
public void Put(long id, [FromBody] Note note)
{
Note note1 = _context.Notes.Where(x => x.NoteId.Equals(note.NoteId)).FirstOrDefault();
note1.Name = note.Name;
note1.IsDone = note.IsDone;
note1.Due = note.Due;
_context.SaveChanges();
}
// DELETE api/<NotesController>/5
[HttpDelete("{id}")]
public void Delete(long id)
{
Note note1 = _context.Notes.Where(x => x.NoteId.Equals(id)).FirstOrDefault();
_context.Notes.Remove(note1);
_context.SaveChanges();
}
}
We are able to reproduce the reported issue at our end also. We have confirmed it as a bug and logged the defect report “Can't add a row in SFGrid when Database table is empty” for the same. Thank you for taking the time to report this issue and helping us improve our product. At Syncfusion, we are committed to fixing all validated defects (subject to technological feasibility and Product Development Life Cycle ) and including the defect fix in our release which is expected to be rolled out by end of April, 2021. Kindly follow our Syncfusion page for release related information.