【解決方法】実行時にデータベース列の動的プロパティを作成するにはどうすればよいですか?


やあ! Blazor で完全に動的に syncfussion データグリッドを作成したいと考えています。 ハードコーディングされたプロパティの代わりに実行時にプロパティを作成する動的クラスを作成しようとしています。 どうすればそれができるか教えてください。

私が試したこと:

私が試したこと:

ブレイザー
//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);

                }

            }

        }

    }

}

解決策 1

syncfusion を使用したデータグリッドでの動的列生成については、次のリンクにアクセスしてください。
Blazor DataGrid コンポーネントの列 | シンクフュージョン[^]

これがサンプルです。このサンプルをデモンストレーションとしてダウンロードし、要件に応じてカスタマイズします。

[^]

コメント

タイトルとURLをコピーしました