# NeoUI -- DataGrid Quick Reference The DataGrid is NeoUI's advanced data grid with sorting, filtering, pagination, inline editing, server-side data loading, virtual scrolling, and full theming support. ## Installation & Import ```bash dotnet add package NeoUI.Blazor --version 3.6.3 ``` ```razor @using NeoUI.Blazor ``` ## Service Registration ```csharp // Program.cs -- basic (field-based rendering only) builder.Services.AddNeoUIComponents(); // Full features (with template cell rendering) builder.Services.AddNeoUIComponents(); builder.Services.AddScoped(); ``` --- ## Usage Patterns ### 1. Simple DataGrid ```razor @code { private List products = new(); protected override async Task OnInitializedAsync() => products = await LoadProducts(); } ``` ### 2. DataGrid with Custom Cell Templates Requires `IDataGridTemplateRenderer` in DI. ```razor
@customer.Name[0] @customer.Name
@customer.Status
``` ### 3. Server-Side DataGrid ```razor @code { private async Task> LoadDataAsync(DataGridDataRequest request) { var query = _context.Orders.AsQueryable(); foreach (var sort in request.SortDescriptors) { query = sort.Direction == DataGridSortDirection.Ascending ? query.OrderBy(o => EF.Property(o, sort.Field)) : query.OrderByDescending(o => EF.Property(o, sort.Field)); } var totalCount = await query.CountAsync(); var items = await query.Skip(request.StartIndex).Take(request.Count).ToListAsync(); return new DataGridDataResponse { Items = items, TotalCount = totalCount }; } } ``` ### 4. Selection ```razor @code { private List selectedCustomers = new(); private void HandleSelectionChanged(IReadOnlyCollection selection) => selectedCustomers = selection.ToList(); } ``` Selection modes: `None` | `Single` | `Multiple` ### 5. Inline Editing (Transactions) ```razor ``` ### 6. State Persistence ```razor @code { private DataGridState gridState = new(); protected override async Task OnInitializedAsync() => gridState = await LocalStorage.GetItemAsync("grid-state") ?? new(); private async Task SaveState(DataGridState state) => await LocalStorage.SetItemAsync("grid-state", state); } ``` ### 7. Advanced: Frozen Columns, Row Grouping, Virtual Scrolling ```razor ``` --- ## DataGridColumn Parameters | Parameter | Type | Description | |-----------|------|-------------| | `Field` | `string` | Property name to bind | | `Header` | `string` | Column header text | | `Sortable` | `bool` | Enable column sorting | | `Filterable` | `bool` | Enable column filtering | | `Editable` | `bool` | Enable inline editing | | `Width` | `int?` | Column width in pixels | | `MinWidth` | `int?` | Minimum width in pixels | | `Frozen` | `bool` | Freeze column (left) | | `FrozenRight` | `bool` | Freeze column (right) | | `CellTemplate` | `RenderFragment` | Custom cell renderer | | `HeaderTemplate` | `RenderFragment` | Custom header renderer | | `Class` | `string?` | CSS classes for cells | --- ## DataGrid Parameters (Key) | Parameter | Type | Default | Description | |-----------|------|---------|-------------| | `Data` | `IEnumerable?` | | Client-side data | | `PagingMode` | `DataGridPagingMode` | `Client` | Client or Server paging | | `PageSize` | `int` | `20` | Rows per page | | `SelectionMode` | `DataGridSelectionMode` | `None` | Row selection mode | | `Sortable` | `bool` | `true` | Global sort toggle | | `Filterable` | `bool` | `false` | Global filter toggle | | `Editable` | `bool` | `false` | Inline editing | | `VirtualScrolling` | `bool` | `false` | Virtual rows | | `Density` | `DataGridDensity` | `Compact` | Row density (`Compact`, `Medium`, `Spacious`). `Comfortable` is an `[Obsolete]` alias for `Medium`. | | `Theme` | `DataGridTheme` | `Shadcn` | Visual theme | | `VisualStyle` | `DataGridStyle` | `Default` | Striped/Bordered/etc. | | `OnDataRequest` | `Func, Task>>` | | Server-side handler | | `OnSelectionChanged` | `EventCallback>` | | Selection callback | | `@bind-State` | `DataGridState` | | State binding | | `OnStateChanged` | `EventCallback` | | State changed callback | --- ## Theming ### High-Level Presets ```razor ... ``` Themes: `Shadcn` | `Alpine` | `Balham` | `Material` | `Quartz` Density: `Compact` | `Medium` | `Spacious` (`Comfortable` is `[Obsolete]` alias for `Medium`) VisualStyle: `Default` | `Striped` | `Bordered` | `Minimal` ### Fine-Grained Parameters ```razor ``` Key theme parameters: `AccentColor`, `BackgroundColor`, `ForegroundColor`, `BorderColor`, `HeaderBackgroundColor`, `RowHoverColor`, `OddRowBackgroundColor`, `SelectedRowBackgroundColor`, `RowHeight`, `HeaderHeight`, `FontSize`, `FontFamily`, `HeaderFontWeight`, `Spacing`, `BorderRadius` ### CSS Override ```razor ... ``` --- ## Sub-Pages (Demo Site) | Path | Description | |------|-------------| | `/components/datagrid` | Overview | | `/components/datagrid/basic` | Basic usage | | `/components/datagrid/templating` | Custom cell templates | | `/components/datagrid/selection` | Row selection | | `/components/datagrid/transactions` | Inline editing | | `/components/datagrid/sorting` | Sorting & filtering | | `/components/datagrid/state` | State persistence | | `/components/datagrid/server-side` | Server-side loading | | `/components/datagrid/advanced` | Frozen columns, grouping, virtual scroll | | `/components/datagrid/theming` | Custom themes |