Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
Hi there! I want to create syncfussion datagrid fully dynamically in Blazor. I'm trying to create a dynamic class that creates properties at runtime instead of hard-coded properties. Please guide me on how can I do that.

What I have tried:

What I tried:

BLAZOR
//Sp Class

 public class Sp : DynamicObject

    {

        Dictionary<string, object=""> properties = new Dictionary<string, object="">();

        public override bool TryGetMember(GetMemberBinder binder, out object result)

        {

            string name = binder.Name;

            return properties.TryGetValue(name, out result);

        }

        public override bool TrySetMember(SetMemberBinder binder, object value)

        {

            properties[binder.Name] = value;

            return true;

        }

        public override IEnumerable<string> GetDynamicMemberNames()

        {

            return properties?.Keys;

        }

    }



//ApplicationDbContext

public class ApplicationDbContext:DbContext

    {

        public ApplicationDbContext(DbContextOptions<applicationdbcontext> options): base(options)

        {



        }

        public DbSet<sp> DisplaySp { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)

        {

            modelBuilder.Entity<sp>().HasNoKey();

            base.OnModelCreating(modelBuilder);

        }

    }



//Controller Side

[HttpGet]

        [Route("Getspdetails")]

        public Task<list<sp>> GetData()

        {

            var result = _services.Get();

            return result;

        }



//Service 

public async Task<list<sp>> Get()

        {

            var data = _context.DisplaySp.FromSqlRaw("Execute Tbl_DailySaleRpSP @nType = 0,@nsType = 2").ToList();

            return data;

        }



//Razor Page

<sfgrid datasource="@Spdetails" allowpaging="true" allowfiltering="true" allowsorting="true">

        <gridpagesettings pagesize="10">

        <gridaggregates>

            <gridaggregate>

                <gridaggregatecolumns>

                    @foreach (var col in columns)

                    {

                        <gridaggregatecolumn field="@col" type="AggregateType.Sum">

                            <footertemplate>

                                @{

                                    var aggregate = (context as AggregateTemplateContext);

                                    <div>

                                        <p>@aggregate?.Sum</p>

                                    </div>

                                }

                            

                        

                    }

                

            

        

    

@code {

    public List<sp> Spdetails { get; set; } = new List<sp>();

    public List<string> columns { get; set; } = new List<string>();

    protected override async Task OnInitializedAsync()

    {

        Spdetails = await Http.GetFromJsonAsync<list<sp>>("Getspdetails");

        if (Spdetails != null && Spdetails.Count > 0) {

            PropertyInfo[] props = typeof(Sp).GetProperties();

            foreach (var prop in props)

            {

                if (prop.PropertyType == typeof(int) || prop.PropertyType == typeof(double) || prop.PropertyType == typeof(decimal))

                {

                    columns.Add(prop.Name);

                }

            }

        }

    }

}
Posted
Updated 12-Nov-23 4:07am
v2
Comments
Graeme_Grant 11-Nov-23 21:21pm    
Did you know that Syncfusion have a support forum: Blazor Forums | Syncfusion[^]

1 solution

You can visit the following link for dynamic columns generation in datagrid using syncfusion:
Columns in Blazor DataGrid Component | Syncfusion[^]
Here is the sample, download this sample as a demonstration and customize it according to your requirements.
https://www.syncfusion.com/downloads/support/forum/164922/ze/DataGrid960377213[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900