Recursive AI

Show data in a read-only table in C#

OneApp is a free, open-source framework for apps in .NET whose UI is HTML and CSS, drawn natively with no web view on Windows, macOS, Linux, iOS and Android, and run in the browser, from Recursive AI. MIT or Apache-2.0, no native DLLs to ship.

This example shows an array of invoice records as a table. TableBody renders one row per record. This example uses OneApp’s desktop host on Linux, Windows, or macOS on Apple silicon.

Terminal
dotnet add package RecursiveAI.OneApp.Kit
dotnet add package RecursiveAI.OneApp
dotnet add package RecursiveAI.OneUI

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

HTML
<!-- Invoices: array in InvoicesPage.cs. -->
<Table>
  <TableCaption>
    Recent invoices (USD)
  </TableCaption>
  <TableBody>
    <template each="{Invoices}"
              item="inv" key="{inv.Id}">
      <TableRow>
        <TableCell>{inv.Id}</TableCell>
        <TableCell>{inv.Status}</TableCell>
        <TableCell>{inv.Amount}</TableCell>
      </TableRow>
    </template>
  </TableBody>
</Table>

The example is the full program below: unzip it and dotnet run.

The template makes one row per invoice; the full example adds column headings.

Full program

OneApp hosts the window, OneApp.Kit supplies the controls, and OneUI compiles the .html component.

The zip contains the three files below and Demo.csproj. To make the project by hand, create a .NET 10 console app and install the packages above. The example’s project references are:

XML
<PackageReference
  Include="RecursiveAI.OneApp"
  Version="1.1.1" />
<PackageReference
  Include="RecursiveAI.OneApp.Kit"
  Version="1.1.1" />
<PackageReference
  Include="RecursiveAI.OneUI"
  Version="1.1.0" />

OneUI compiles InvoicesPage.html into the other half of the partial class in InvoicesPage.cs. Save these files beside the project file.

InvoicesPage.html:

HTML
<Theme>
  <main style="padding: 24px">
    <Table>
      <TableCaption>Recent invoices (USD)</TableCaption>
      <TableHeader>
        <TableRow>
          <TableHead>Invoice</TableHead>
          <TableHead>Status</TableHead>
          <TableHead
            Align="{OneApp.Kit.CellAlign.End}">
            Amount
          </TableHead>
        </TableRow>
      </TableHeader>
      <TableBody>
        <template each="{Invoices}" item="inv" key="{inv.Id}">
          <TableRow>
            <TableCell>{inv.Id}</TableCell>
            <TableCell>{inv.Status}</TableCell>
            <TableCell Align="{OneApp.Kit.CellAlign.End}">{inv.Amount}</TableCell>
          </TableRow>
        </template>
      </TableBody>
    </Table>
  </main>
</Theme>

InvoicesPage.cs:

C#
public partial class InvoicesPage
{
    public record Invoice(string Id, string Status, string Amount);
 
    public Invoice[] Invoices { get; } =
    [
        new("INV-101", "Paid", "$120.00"),
        new("INV-102", "Open", "$45.50"),
    ];
}

Program.cs opens the native window. Checked on Linux (X11) with OneApp 1.1.1:

C#
using OneApp;
 
using var host = AppHost.Create(new AppOptions(new InvoicesPage())
{
    Title = "Invoices",
    Width = 420,
    Height = 255,
});
host.Run();

The window shows this table, with the caption Recent invoices (USD) below it:

InvoiceStatusAmount
INV-101Paid$120.00
INV-102Open$45.50

If the headings appear but the rows don’t, check that Invoices contains records and that the loop is inside TableBody.

Show data in a read-only table in C#: example window

Variations

To emphasize invoice IDs without making them editable, replace the ID cell in InvoicesPage.html:

HTML
<TableCell Medium="true">{inv.Id}</TableCell>

The short answer

Use TableHeader and TableHead for the headings, then loop over the C# records in TableBody, rendering a TableRow with TableCell elements for each record. The cells display text; there is no edit mode to turn off.

How do I display C# data in a read-only desktop table?

The sample uses an in-memory array, but the table only needs the collection you give it. Each invoice supplies a stable, unique Id as the template key, so the row’s identity doesn’t depend on where it appears in the list. Keep the header and the cell order the same, and put the unit in the caption instead of making the reader guess which currency the amounts use. CellAlign.End lines up the amounts at their trailing edge; use it on the heading too.

Does a read-only table sort or page the data?

The kit renders table elements with their row and column semantics. It doesn’t sort, page, resize columns, or virtualize rows. Use it for a modest set of records such as a recent-invoices summary. If you need paging, pass only the current page of records to the template; if you need sorting, order the records before handing them to the table. Sort numeric invoice amounts as numbers, before formatting them for display. On a narrow window the table’s container scrolls horizontally; the cells don’t wrap.

All oneapp recipes