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 >
<thead>
<tr>
<th >Text A</th>
<th >Text B</th>
<th >Text C</th>
</tr>
</thead>
<tbody>
@ItemsContent
</tbody>
</table>
</RepeaterContainerTemplate>
<ItemTemplate Context=test>
<tr>
<td >@test["Text_A"]</td>
<td >@test["Text_B"]</td>
<td >@test["Text_C"]</td>
</tr>
</ItemTemplate>
</Repeater2>
CodePudding user response:
You can use System.Data.DataTable in Blazor application like this
This is your html
@using System.Data
<table >
<thead>
<tr>
<th >Text A</th>
<th >Text B</th>
<th >Text C</th>
</tr>
</thead>
<tbody>
@foreach (DataRow row in table.Rows)
{
<td >@row["TextA"]</td>
<td >@row["TextB"]</td>
<td >@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);
}
}
CodePudding user response:
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.