You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于模型列表使用FluentUI展示带样式DataGrid的技术咨询

基于自有模型的带样式DataGrid解决方案

一、推荐的带样式DataGrid库(支持模型绑定)

  • Blazor DataGrid(.NET技术栈):原生支持强类型模型绑定,自带基础样式,也能轻松自定义外观。
  • React Table(React技术栈):轻量易扩展,支持基于类/接口配置列,社区有大量现成样式主题可用。
  • Vuetify v-data-table(Vue技术栈):Vue生态成熟组件,内置美观样式,直接绑定模型数组即可使用。

二、实现示例(以.NET Blazor + EF Core从数据库填充为例)

1. 定义数据模型

// 与数据库表字段一一对应
public class Product
{
    public int Id { get; set; }
    public string ProductName { get; set; }
    public decimal Price { get; set; }
    public DateTime CreateTime { get; set; }
}

2. 从数据库查询模型列表

// 数据服务层代码
public class ProductService
{
    private readonly AppDbContext _dbContext;

    public ProductService(AppDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    public async Task<List<Product>> GetAllProductsAsync()
    {
        // EF Core查询数据库,返回模型列表
        return await _dbContext.Products.ToListAsync();
    }
}

3. 绑定DataGrid展示数据

@page "/product-list"
@inject ProductService _productService

<h3>产品数据列表</h3>

<!-- 绑定模型列表,配置列映射 -->
<DataGrid TItem="Product" Items="@_products" ShowPager="true" PageSize="10">
    <Columns>
        <PropertyColumn Property="@p => p.Id" Title="ID" />
        <PropertyColumn Property="@p => p.ProductName" Title="产品名称" />
        <PropertyColumn Property="@p => p.Price" Title="售价" Format="C" />
        <PropertyColumn Property="@p => p.CreateTime" Title="创建时间" Format="yyyy-MM-dd HH:mm" />
    </Columns>
</DataGrid>

@code {
    private List<Product> _products;

    protected override async Task OnInitializedAsync()
    {
        // 从数据库获取数据
        _products = await _productService.GetAllProductsAsync();
    }
}

4. 样式调整

Blazor DataGrid自带默认样式,若需自定义,直接写CSS覆盖即可:

/* 自定义DataGrid表头样式 */
.datagrid-header-cell {
    background-color: #f0f2f5;
    font-weight: 600;
    padding: 10px;
    border-bottom: 2px solid #e0e0e0;
}

/* 自定义单元格样式 */
.datagrid-cell {
    padding: 10px;
    border-bottom: 1px solid #f0f0f0;
}

三、数据库填充类列表的核心流程

不管用哪个技术栈,核心步骤都是一致的:

  • 创建与数据库表映射的模型类,属性字段和表字段保持一致。
  • 通过ORM框架(如EF Core、Hibernate)或原生SQL执行查询,将数据库结果转换成模型类的集合。
  • 将模型集合赋值给DataGrid的数据源属性,完成数据绑定。
  • 配置列规则:指定每个列对应模型的哪个属性、显示标题、数据格式化方式等。

四、无样式问题排查

你搜到的DataGrid无样式,通常是这两个原因:

  • 没引入组件库的官方CSS文件:比如用Blazor DataGrid需要在项目中添加对应的样式引用,否则只有功能没有样式。
  • 用了纯逻辑型DataGrid组件:有些组件只提供数据绑定功能,不带内置样式,需要你自己编写CSS或者搭配Bootstrap这类UI框架的样式使用。

内容的提问来源于stack exchange,提问作者gal mor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 05:12:23