Grid
Use Grid
to render items in a grid pattern.
Usage
Basic usage
var grid = new Grid();
// Add columns
grid.AddColumn();
grid.AddColumn();
grid.AddColumn();
// Add header row
grid.AddRow(new string[]{"Header 1", "Header 2", "Header 3"});
grid.AddRow(new string[]{"Row 1", "Row 2", "Row 3"});
// Write to Console
AnsiConsole.Write(grid);
Align and style items within cells
var grid = new Grid();
// Add columns
grid.AddColumn();
grid.AddColumn();
grid.AddColumn();
// Add header row
grid.AddRow(new Text[]{
new Text("Header 1", new Style(Color.Red, Color.Black)).LeftJustified(),
new Text("Header 2", new Style(Color.Green, Color.Black)).Centered(),
new Text("Header 3", new Style(Color.Blue, Color.Black)).RightJustified()
});
// Add content row
grid.AddRow(new Text[]{
new Text("Row 1").LeftJustified(),
new Text("Row 2").Centered(),
new Text("Row 3").RightJustified()
});
// Write centered cell grid contents to Console
AnsiConsole.Write(grid);
Embed a grid within a grid
var grid = new Grid();
// Add columns
grid.AddColumn();
grid.AddColumn();
grid.AddColumn();
// Add header row
grid.AddRow(new Text[]{
new Text("Header 1", new Style(Color.Red, Color.Black)).LeftJustified(),
new Text("Header 2", new Style(Color.Green, Color.Black)).Centered(),
new Text("Header 3", new Style(Color.Blue, Color.Black)).RightJustified()
});
var embedded = new Grid();
embedded.AddColumn();
embedded.AddColumn();
embedded.AddRow(new Text("Embedded I"), new Text("Embedded II"));
embedded.AddRow(new Text("Embedded III"), new Text("Embedded IV"));
// Add content row
grid.AddRow(
new Text("Row 1").LeftJustified(),
new Text("Row 2").Centered(),
embedded
);
// Write centered cell grid contents to Console
AnsiConsole.Write(grid);
References
- Grid Class