System.data.datable可在大火中使用吗?

发布于 2025-01-24 06:28:34 字数 1137 浏览 3 评论 0原文

我是大麻店的新手,并且正试图使用​​它。 我尝试重新创建中继器,并试图使用System.Data.DataTable来绑定它,但似乎不可能这样做,而且我也找不到如何调用行的方法。

关于如何在Glazor中使用DataTable的回旋方式吗?

 <Repeater2 Items=@dtTest>
    <RepeaterContainerTemplate Context=ItemsContent>
        <table class="table table-condensed table-bordered">
            <thead>
                <tr>
                    <th class="text-center">Text A</th>
                    <th class="text-center">Text B</th>
                    <th class="text-center">Text C</th>
                </tr>
             </thead>
             <tbody>
                   @ItemsContent
             </tbody>
         </table>
     </RepeaterContainerTemplate>
     <ItemTemplate Context=test>
         <tr>
             <td class="text-center">@test["Text_A"]</td>
             <td class="text-center">@test["Text_B"]</td>
             <td class="text-center">@test["Text_C"]</td>
         </tr>                 
     </ItemTemplate>
 </Repeater2>

I'm new to blazor and was trying to use it.
I tried recreating the repeater and was trying to bind it using System.Data.DataTable but it seems to be impossible to do it and also i can't find a way on how to call the rows.

Is there a roundabout way on how to use DataTable in Blazor?

 <Repeater2 Items=@dtTest>
    <RepeaterContainerTemplate Context=ItemsContent>
        <table class="table table-condensed table-bordered">
            <thead>
                <tr>
                    <th class="text-center">Text A</th>
                    <th class="text-center">Text B</th>
                    <th class="text-center">Text C</th>
                </tr>
             </thead>
             <tbody>
                   @ItemsContent
             </tbody>
         </table>
     </RepeaterContainerTemplate>
     <ItemTemplate Context=test>
         <tr>
             <td class="text-center">@test["Text_A"]</td>
             <td class="text-center">@test["Text_B"]</td>
             <td class="text-center">@test["Text_C"]</td>
         </tr>                 
     </ItemTemplate>
 </Repeater2>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苏大泽ㄣ 2025-01-31 06:28:34

这不是在大火中做事的方法。我将Dapper用于我的SQL查询,并且我的数据方法返回键入list。这使得Linq可以在标记中的循环中对我的查询进行排序,过滤或以其他方式操纵我的查询结果非常容易。这比在Winforms或WebForms中所做的事情要容易得多,在Winforms或WebForms中,您必须在CodeBehind中使用数据表,行等。

您不必创建一个中继器或任何ASP.NET Web表单列表对象,因为现在您可以在标记中使用C#逻辑来决定渲染内容以及如何渲染,并且根据我的经验,这是无限的。

This is not how to do things in Blazor. I use Dapper for my SQL queries, and my data methods return a typed List. This makes it very easy with Linq to sort, filter, or otherwise manipulate my query results in foreach loops in the markup. This is much easier than the way things used to be done in Winforms or Webforms, where you had to work with data tables, rows and so on in codebehind.

You don't have to create a repeater or any of the asp.net Web Forms listing objects, because now you can use C# logic in your markup to decide what and how to render, and in my experience this is infinitely better.

兰花执着 2025-01-31 06:28:34

您可以使用System.Data.Databable在Glazor应用程序中,

这是您的HTML,

@using System.Data

<table class="table table-condensed table-bordered">
    <thead>
        <tr>
            <th class="text-center">Text A</th>
            <th class="text-center">Text B</th>
            <th class="text-center">Text C</th>
        </tr>
    </thead>
    <tbody>
        @foreach (DataRow row in table.Rows)
        {
            <td class="text-center">@row["TextA"]</td>
            <td class="text-center">@row["TextB"]</td>
            <td class="text-center">@row["TextC"]</td>
        }
    </tbody>
</table>

这是您的代码

@code {    
    private System.Data.DataTable table;
    
    protected override void OnInitialized()
    {
        table = new();

        table.Columns.Add("TextA");
        table.Columns.Add("TextB");
        table.Columns.Add("TextC");

        var row = table.NewRow();
        row["TextA"] = "Text_A";
        row["TextB"] = "Text_B";
        row["TextC"] = "Text_C";

        table.Rows.Add(row);
    }    
}

You can use System.Data.DataTable in Blazor application like this

This is your html

@using System.Data

<table class="table table-condensed table-bordered">
    <thead>
        <tr>
            <th class="text-center">Text A</th>
            <th class="text-center">Text B</th>
            <th class="text-center">Text C</th>
        </tr>
    </thead>
    <tbody>
        @foreach (DataRow row in table.Rows)
        {
            <td class="text-center">@row["TextA"]</td>
            <td class="text-center">@row["TextB"]</td>
            <td class="text-center">@row["TextC"]</td>
        }
    </tbody>
</table>

This is your code behind

@code {    
    private System.Data.DataTable table;
    
    protected override void OnInitialized()
    {
        table = new();

        table.Columns.Add("TextA");
        table.Columns.Add("TextB");
        table.Columns.Add("TextC");

        var row = table.NewRow();
        row["TextA"] = "Text_A";
        row["TextB"] = "Text_B";
        row["TextC"] = "Text_C";

        table.Rows.Add(row);
    }    
}
梦冥 2025-01-31 06:28:34

您可以在互联网上的许多网站上找到与此类似的东西,因此我基本上只是在反思现有的东西。

mytable.razor

@typeparam TRow

@if (this.HeaderRow is not null && this.DataRow is not null && this.DataTable is not null && this.Show)
{
    <table @attributes=UserAttributes>
        <thead>
        <tr>
            @this.HeaderRow
        </tr>
        </thead>
        <tbody>
        @foreach (var item in DataTable)
        {
            <tr>
                @this.DataRow(item)
            </tr>
        }
        </tbody>
    </table>
}

@code {
    [Parameter] public bool Show { get; set; }
    [Parameter] public IEnumerable<TRow>? DataTable { get; set; }
    [Parameter] public RenderFragment? HeaderRow { get; set; }
    [Parameter] public RenderFragment<TRow>? DataRow { get; set; }
    [Parameter(CaptureUnmatchedValues = true)] public IDictionary<string, object> UserAttributes { get; set; } = new Dictionary<string, object>();
}

,这是使用组件的fetchdata的版本。

@page "/fetchdata"
<PageTitle>Weather forecast</PageTitle>
@inject WeatherForecastService ForecastService

<h1>Weather forecast</h1>

<p>This component demonstrates fetching data from a service.</p>

<BaseTable TRow=WeatherForecast DataTable=this.forecasts Show=this.show class="table">
    <HeaderRow>
        <th>Date</th>
        <th>Temp. (C)</th>
        <th>Temp. (F)</th>
        <th>Summary</th>
    </HeaderRow>
    <DataRow>
        <td>@context.Date.ToShortDateString()</td>
        <td>@context.TemperatureC</td>
        <td>@context.TemperatureF</td>
        <td>@context.Summary</td>
    </DataRow>
</BaseTable>

@code {
    private WeatherForecast[]? forecasts;
    private bool show => forecasts is not null;
    protected override async Task OnInitializedAsync()
    {
        forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
    }
}

请注意,我将始终使用适当的视图服务进行我的数据管理,因此WeatherForecast []将生活在该服务中而不是UI中。

You can find something similar to this on many sites on the Internet, so I'm just basically regurgitating what's already out there.

MyTable.razor

@typeparam TRow

@if (this.HeaderRow is not null && this.DataRow is not null && this.DataTable is not null && this.Show)
{
    <table @attributes=UserAttributes>
        <thead>
        <tr>
            @this.HeaderRow
        </tr>
        </thead>
        <tbody>
        @foreach (var item in DataTable)
        {
            <tr>
                @this.DataRow(item)
            </tr>
        }
        </tbody>
    </table>
}

@code {
    [Parameter] public bool Show { get; set; }
    [Parameter] public IEnumerable<TRow>? DataTable { get; set; }
    [Parameter] public RenderFragment? HeaderRow { get; set; }
    [Parameter] public RenderFragment<TRow>? DataRow { get; set; }
    [Parameter(CaptureUnmatchedValues = true)] public IDictionary<string, object> UserAttributes { get; set; } = new Dictionary<string, object>();
}

And here's a version of FetchData that uses the component.

@page "/fetchdata"
<PageTitle>Weather forecast</PageTitle>
@inject WeatherForecastService ForecastService

<h1>Weather forecast</h1>

<p>This component demonstrates fetching data from a service.</p>

<BaseTable TRow=WeatherForecast DataTable=this.forecasts Show=this.show class="table">
    <HeaderRow>
        <th>Date</th>
        <th>Temp. (C)</th>
        <th>Temp. (F)</th>
        <th>Summary</th>
    </HeaderRow>
    <DataRow>
        <td>@context.Date.ToShortDateString()</td>
        <td>@context.TemperatureC</td>
        <td>@context.TemperatureF</td>
        <td>@context.Summary</td>
    </DataRow>
</BaseTable>

@code {
    private WeatherForecast[]? forecasts;
    private bool show => forecasts is not null;
    protected override async Task OnInitializedAsync()
    {
        forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
    }
}

Note that I would always use a proper view service for my data management so WeatherForecast[] would live in that service and not in the UI.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文