Skip to content

Extras

DataGrid

Bit.BlazorUI.ExtrasDataTableTreeGridTreeList

BitDataGrid displays an information-rich data table and gives people the tools to work through it: sorting, quick search and per-column filters, paging, grouping with aggregates, selection, inline editing, resizable/reorderable/frozen columns, master-detail and tree rows, keyboard cell navigation, row and column virtualization, and CSV/Excel export. Bind a list, an IQueryable executed at the source, or a callback for server-side paging or infinite scrolling. Use a data grid when information density is critical.

Interaction & state

Dragging rows into a new order, row and cell events with a custom context menu, the programmatic API and its change events, and saving and restoring the grid's state.

Row reordering

RowReorderable adds a ⠿ handle: drag it onto another row (mouse, touch or pen), or focus it and press ↑/↓. The grid moves the item in the bound list and raises OnRowReorder. It pauses while the view is sorted, filtered or grouped.

The move is made in place only when Items is a mutable list (IList<T>); otherwise OnRowReorder's FromIndex and ToIndex are null and the new order is yours to apply from DraggedItem and TargetItem. Persist the order in the handler. Because a move is made by index in the bound list, the handle also pauses while a search is on, and is not offered in a tree, with OnRead, OnLoadMore or an IQueryable. That is why this grid sets Sortable="false": a header click would sort the rows and pause the handle. A disabled grid moves nothing.

Reorder
ID
Name
Category
Price
1
Vintage Gadget 212
Home
$171.75
2
Eco Notebook 554
Clothing
$382.16
3
Classic Gadget 181
Toys
$813.02
4
Vintage Blender 267
Electronics
$310.12
5
Vintage Jacket 234
Electronics
$55.50
6
Smart Drone 229
Electronics
$603.51
7
Eco Widget 428
Books
$72.97
8
Pro Blender 549
Electronics
$825.55
9
Deluxe Mug 514
Clothing
$615.44
10
Mini Jacket 367
Grocery
$585.87
11
Vintage Gadget 192
Home
$334.92
12
Mini Mug 788
Toys
$712.80

Cell events & context menu

OnCellClick, OnCellDoubleClick and OnCellContextMenu report the row, the column, the value and the mouse event; right-click a cell for a custom menu. OnRowClick and OnRowDoubleClick report the row alone.

The arguments carry the row (Item), the Column with its ColumnId and ColumnTitle, the cell's raw Value (not its formatted text) and the Mouse event, whose coordinates place the menu here. While OnCellContextMenu is set the browser's own menu is suppressed on the data cells.

A click on a control inside a cell belongs to the control: a button, link or field of a template, an ARIA widget, an element in the tab order, an element marked data-bit-interactive, the grid's own checkboxes and toggles, and anything in a dialog or a grid inside the cell raise none of the row and cell events, select nothing and expand nothing - and a right-click there keeps the browser's menu. Mark an element of a template data-bit-interactive to treat it so (an icon with an @onclick), or data-bit-interactive="false" to let a control's clicks reach the cell and the row. A disabled grid raises none of these events. The menu itself is the page's own markup (see the stylesheet tab); the grid only reports the click.

1
Vintage Gadget 212
Home
$171.75
2
Eco Notebook 554
Clothing
$382.16
3
Classic Gadget 181
Toys
$813.02
4
Vintage Blender 267
Electronics
$310.12
5
Vintage Jacket 234
Electronics
$55.50
6
Smart Drone 229
Electronics
$603.51
7
Eco Widget 428
Books
$72.97
8
Pro Blender 549
Electronics
$825.55
9
Deluxe Mug 514
Clothing
$615.44
10
Mini Jacket 367
Grocery
$585.87
11
Vintage Gadget 192
Home
$334.92
12
Mini Mug 788
Toys
$712.80
13
Classic Mug 124
Books
$267.79
14
Compact Gadget 195
Clothing
$325.60
15
Deluxe Speaker 932
Sports
$281.65
16
Eco Widget 307
Grocery
$982.55
17
Mini Blender 740
Electronics
$597.20
18
Pro Blender 952
Sports
$132.04
19
Classic Drone 459
Electronics
$906.58
20
Mega Mug 839
Sports
$282.70
21
Mega Speaker 349
Books
$830.28
22
Modern Mug 947
Home
$886.86
23
Mini Backpack 199
Sports
$349.39
24
Mini Speaker 940
Electronics
$60.13
25
Deluxe Lamp 430
Home
$760.77
26
Eco Mug 633
Toys
$388.23
27
Compact Camera 900
Electronics
$300.37
28
Mega Mug 366
Grocery
$749.69
29
Mini Jacket 409
Books
$604.08
30
Premium Camera 361
Grocery
$352.80
31
Mega Gadget 949
Books
$654.24
32
Mega Lamp 485
Books
$787.62
33
Compact Backpack 302
Clothing
$540.00
34
Pro Mug 551
Grocery
$45.94
35
Ultra Mug 514
Clothing
$484.40
36
Pro Camera 944
Home
$211.34
37
Mini Notebook 156
Electronics
$900.67
38
Premium Jacket 198
Electronics
$651.34
39
Classic Drone 215
Sports
$290.56
40
Mini Sneaker 570
Sports
$897.23

Click, double-click or right-click any cell.

Programmatic control

Everything the UI does is public API: SortByAsync, ApplyFilterAsync, GroupByAsync, GoToPageAsync, SetPageSizeAsync, MoveColumnAsync, SearchAsync, RefreshAsync and more. OnSortChange, OnFilterChange, OnGroupChange and OnPageChange report each change however it was made. OnViewChange hands over the rows of the view across every page - what a map or a chart of the same rows needs - and RevealItemAsync brings any of them into sight, going to its page and opening its group.

The methods take a column by its id - the ColumnId, or the property path of a bound column (Price, Address.City). SortByAsync replaces the sort unless additive is true, and BitDataGridSortDirection.None removes the column's sort; ApplyFilterAsync replaces the column's filter, ApplyRangeFilterAsync sets a from-inclusive, to-exclusive range; GoToPageAsync takes a 1-based page and clamps it; SetPageSizeAsync and SearchAsync go back to the first page; MoveColumnAsync takes a 0-based position among all the columns. ActiveSorts, ActiveFilters, ActiveGroups, ActiveSearch, CurrentPage, TotalPages and TotalCount read the current view.

The change events fire for the user's changes and the API's alike, with the new descriptors (OnPageChange with the 1-based page, also when the page size changes). OnViewChange tells rows apart by KeyField (by reference without one), so give it a key when the source hands over new instances of the same rows; it is raised with a list, a tree or an IQueryable, while OnRead and OnLoadMore grids hold only a window of the view - GetViewItemsAsync reads it all there, and GetViewItems returns the loaded rows. RevealItemAsync returns false when the row is not in the view (filtered out, say). RefreshAsync recomputes the view, or reads it again from a server.


1
Vintage Gadget 212
Home
$171.75
131
2
Eco Notebook 554
Clothing
$382.16
130
3
Classic Gadget 181
Toys
$813.02
269
4
Vintage Blender 267
Electronics
$310.12
291
5
Vintage Jacket 234
Electronics
$55.50
390
6
Smart Drone 229
Electronics
$603.51
257
7
Eco Widget 428
Books
$72.97
359
8
Pro Blender 549
Electronics
$825.55
6
9
Deluxe Mug 514
Clothing
$615.44
22
10
Mini Jacket 367
Grocery
$585.87
367
1–10 of 200
Page 1 of 20

The grid reports every view change here.

State persistence

OnStateChange hands over a GetState snapshot - page and page size, search, sorts, filters, groups and which are open, and each column's order, width and visibility - after every change. Store it, then give it to ApplyStateAsync on a new grid; recreate this one to see. A JSON round trip is fine: filter values are converted back to their column's type.

The snapshot is a plain BitDataGridState - CurrentPage, PageSize (null while the PageSize parameter applies), Search, Sorts, Filters, Groups, GroupsCollapsed with the GroupExpansionOverrides, and a BitDataGridColumnState per column (ColumnId, Visible, the resized Width and the Order) - so it serializes as it is, to local storage or a user profile. It does not hold the selection, the open detail rows or the open tree nodes (a tree node is known only by its KeyField, which need not serialize). Columns are matched by id, so a template-only column needs a ColumnId of its own for its layout to come back.

Apply the state once the grid has rendered and its columns have registered - from OnAfterRenderAsync, as here. ApplyStateAsync does not raise OnStateChange; every other change does, the user's and the API's alike, after the grid has re-rendered.

Sort, filter, resize, move or hide columns - every change is saved.

1
Vintage Gadget 212
Home
$171.75
131
2
Eco Notebook 554
Clothing
$382.16
130
3
Classic Gadget 181
Toys
$813.02
269
4
Vintage Blender 267
Electronics
$310.12
291
5
Vintage Jacket 234
Electronics
$55.50
390
6
Smart Drone 229
Electronics
$603.51
257
7
Eco Widget 428
Books
$72.97
359
8
Pro Blender 549
Electronics
$825.55
6
9
Deluxe Mug 514
Clothing
$615.44
22
10
Mini Jacket 367
Grocery
$585.87
367
1–10 of 120
Page 1 of 12

Feedback

Found a mistake, a gap, or something that could be clearer? Every page and every component is one click from its source.