Blazor 中不直接支持 System.Data.DataTable,但可以通过使用 Nuget 包“System.Data.Common”实现类似的功能。以下是一个使用 DataTable 的示例:
首先,将 System.Data.Common 添加到 Blazor 项目中。
创建一个包含 DataTable 的 C# 类,例如:
using System.Data;
public class MyTable
{
public DataTable GetTable()
{
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Rows.Add(1, "John");
table.Rows.Add(2, "Mary");
table.Rows.Add(3, "Jane");
return table;
}
}
@page "/mytable"
@using System.Data.Common
@inject MyTable myTable
Data Table Example
ID
Name
@foreach (DataRow row in myTable.GetTable().Rows)
{
@row["ID"]
@row["Name"]
}
通过这种方式,在 Blazor 中实现 DataTable 和显示数据的功能。