We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

SfGrid not updating grid with current List elements

Hi, i'm trying adding data to List, not to connect with database, but the data grid not updating.


Here is a code sample, the <li> data is updating with current list, but SfGrid still showing empty after adding data to list.

Any help would be greatly appreciated, thanks.


@page "/Test"

<input type="text" @bind="@newPerson.Name" placeholder="Name" />
<input type="number" @bind="@newPerson.Age" placeholder="Age" />
<button @onclick="AddPerson">Add Person</button>


<ul>
    @foreach (var person in people) {
        <li>@person.Name, @person.Age</li>
    }
</ul>
<SfGrid DataSource="@people" />


@code {
    private List<Person> people = new List<Person>();
    private Person newPerson = new Person();


    private void AddPerson() {
        people.Add(newPerson);
        newPerson = new Person();
        StateHasChanged();
    }


    public class Person {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

1 Reply 1 reply marked as answer

MS Monisha Saravanan Syncfusion Team March 16, 2023 12:25 PM UTC


Hi Bonifasius,


Greetings from Syncfusion support.


We would like to inform that after adding the list to the datasource dynamically it is not refreshed properly. So we suggest you to call Grid Refresh after the datasource changes. It will refresh the Grid with newly added records. Kindly check the below attached modified code snippet for your reference.


 

<input type="text" @bind="@newPerson.Name" placeholder="Name" />

<input type="number" @bind="@newPerson.Age" placeholder="Age" />

<button @onclick="AddPerson">Add Person</button>

 

 

<ul>

    @foreach (var person in people) {

        <li>@person.Name, @person.Age</li>

    }

</ul>

<SfGrid DataSource="@people" @ref="Grid" />

 

 

@code {

    SfGrid<Person> Grid;

    private List<Person> people = new List<Person>();

    private Person newPerson = new Person();

 

 

    private void AddPerson() {

        people.Add(newPerson);

        newPerson = new Person();

        Grid.Refresh();

        StateHasChanged();

    }

 

 

    public class Person {

        public string Name { get; set; }

        public int Age { get; set; }

    }

}

 


Please let us know if you have any concerns.


Regards,

Monisha


Marked as answer
Loader.
Up arrow icon