Blazor Grid Selection Overview
With the Ignite UI for Blazor IgbGrid
you can easily select data by using variety of events, rich API or with simple mouse interactions like single select.
Blazor Grid Selection Example
The sample below demonstrates three types of cell selection behaviors in the IgbGrid
. Use the buttons below to enable each of the available selection modes.
Blazor Grid Selection Options
The Ignite UI for Blazor IgbGrid
component provides three different selection modes - Row selection, Cell selection and Column selection. By default only Multi-cell selection mode is enabled in the IgbGrid
. In order to change/enable selection mode you can use RowSelection
, CellSelection
or Selectable
properties.
Blazor Grid Row Selection
Property RowSelection
enables you to specify the following options:
None
- Row selection would be disabled for theIgbGrid
.Single
- Selection of only one row within theIgbGrid
would be available.Multiple
- Multi-row selection would be available by using the row selectors, with a key combination like ctrl + click, or by pressing the space key once a cell is focused.
Go to Row selection topic for more information.
Blazor Grid Cell Selection
Property CellSelection
enables you to specify the following options:
None
- Cell selection would be disabled for theIgbGrid
.Single
- Selection of only one cell within theIgbGrid
would be available.Multiple
- Currently, this is the default state of the selection in theIgbGrid
. Multi-cell selection is available by mouse dragging over the cells, after a left button mouse clicked continuously.
Go to Cell selection topic for more information.
Blazor Grid Column Selection
The Selectable
property enables you to specify the following options for each IgbColumn
. The corresponding column selection will be enabled or disabled if this property is set to true or false, respectively.
This leads to the following three variations:
- Single selection - mouse click over the column cell.
- Multi column selection - holding ctrl + mouse click over the column cells.
- Range column selection - holding shift + mouse click selects everything in between.
Go to Column selection topic for more information.
Blazor Grid Context Menu
Using the ContextMenu
event you can add a custom context menu to facilitate your work with IgbGrid
. With a right click on the grid's body, the event emits the cell on which it is triggered. The context menu will operate with the emitted cell.
If there is a multi-cell selection, we will put logic, which will check whether the selected cell is in the area of the multi-cell selection. If it is, we will also emit the values of the selected cells.
Basically the main function will look like this:
public void RightClick(MouseEventArgs e)
{
this.MenuX = e.ClientX + "px";
this.MenuY = e.ClientY + "px";
}
public void onMenuShow(IgbGridCellEventArgs e)
{
IgbGridCellEventArgsDetail detail = e.Detail;
this.ShowMenu = true;
this.ClickedCell = detail.Cell;
}
The context menu will have the following functions:
- Copy the selected cell's value.
- Copy the selected cell's dataRow.
- If the selected cell is within a multi-cell selection range, copy all the selected data.
public void CopyCellData()
{
this.ShowMenu = false;
this.SelectedData = this.ClickedCell.Value.ToString();
StateHasChanged();
}
public async void CopyRowData()
{
this.ShowMenu = false;
NwindDataItem rowData = this.NwindData.ElementAt(this.ClickedCell.Id.RowIndex);
this.SelectedData = JsonConvert.SerializeObject(rowData);
StateHasChanged();
}
public async void CopyCellsData()
{
this.ShowMenu = false;
var selectedData = await this.grid.GetSelectedDataAsync(true, false);
this.SelectedData = JsonConvert.SerializeObject(selectedData);
StateHasChanged();
}
The IgbGrid
will fetch the copied data and will paste it in a container element.
The template we are going to use to combine the grid with the context menu:
<div class="container vertical">
<div class="wrapper" oncontextmenu="event.preventDefault()">
<IgbGrid AutoGenerate="false"
CellSelection="GridSelectionMode.Multiple"
ContextMenu="onMenuShow"
@oncontextmenu="RightClick"
Name="grid"
@ref="grid"
Data="NwindData">
<IgbColumn Field="ProductID"
Header="Product ID">
</IgbColumn>
<IgbColumn Field="ProductName"
Header="Product Name">
</IgbColumn>
<IgbColumn Field="UnitsInStock"
Header="Units In Stock"
DataType="GridColumnDataType.Number">
</IgbColumn>
<IgbColumn Field="UnitPrice"
Header="Units Price"
DataType="GridColumnDataType.Number">
</IgbColumn>
<IgbColumn Field="Discontinued"
DataType="GridColumnDataType.Boolean">
</IgbColumn>
<IgbColumn Field="OrderDate"
Header="Order Date"
DataType="GridColumnDataType.Date">
</IgbColumn>
</IgbGrid>
<div class="selected-data-area">
@SelectedData
</div>
</div>
@if (ShowMenu)
{
<div id="menu" class="contextmenu" style="left: @MenuX; top: @MenuY">
<span id="copySingleCell" class="item" @onclick="CopyCellData">
<IgbIcon @ref=icon IconName="content_copy" Collection="material"></IgbIcon>Copy Cell Data
</span>
<span id="copyRow" class="item" @onclick="CopyRowData">
<IgbIcon IconName="content_copy" Collection="material"></IgbIcon>Copy Row Data
</span>
<span id="copyMultiCells" class="item" @onclick="CopyCellsData">
<IgbIcon IconName="content_copy" Collection="material"></IgbIcon>Copy Cells Data
</span>
</div>
}
</div>
Select multiple cells and press the right mouse button. The context menu will appear and after selecting Copy cells data the selected data will appear in the right empty box.
The result is:
Known Issues and Limitations
When the grid has no PrimaryKey
set and remote data scenarios are enabled (when paging, sorting, filtering, scrolling trigger requests to a remote server to retrieve the data to be displayed in the grid), a row will lose the following state after a data request completes:
- Row Selection
- Row Expand/collapse
- Row Editing
- Row Pinning
API References
GridCell
Additional Resources
- Row Selection
- Cell Selection
- Paging
- Filtering
- Sorting
- Summaries
- Column Moving
- Virtualization and Performance
Our community is active and always welcoming to new ideas.