I am testing multiple cell selection of Blazor SfGrid. I want to know how to get all selected cells. I tried to use CellSelected and CellDeselected methods to get selected cells. But the CellDeselected looks not working correctly. So I don't know what can do.
Thank you for your reply.
Please look at the image below. There are 4 cells selected, I want to know which cells are selected, then I can update the values of selected cells.
|
<SfButton OnClick="Select" Content="Update Selected"></SfButton>
<SfGrid @ref="GridInstance" DataSource="@Orders" AllowSelection="true" AllowPaging="true">
<GridEvents CellSelected="OnCellSelect" TValue="Order"></GridEvents>
<GridEditSettings AllowEditing="true" Mode="EditMode.Batch"></GridEditSettings>
<GridSelectionSettings Mode="SelectionMode.Cell" Type="Syncfusion.Blazor.Grids.SelectionType.Multiple"></GridSelectionSettings>
<GridColumns>
<GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn>
<GridColumn Field=@nameof(Order.OrderDate) HeaderText=" Order Date" Format="d" Type="ColumnType.Date" TextAlign="TextAlign.Right" Width="130"></GridColumn>
<GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn>
</GridColumns>
</SfGrid>
@code{
public SfGrid<Order> GridInstance { get; set; }
public List<Order> Orders { get; set; }
public List<Tuple<double, double>> SelectedCells { get; set; } = new List<Tuple<double, double>>();
public async Task OnCellSelect(CellSelectEventArgs<Order> Args)
{
if (!SelectedCells.Contains(new Tuple<double, double>(Args.RowIndex, Args.CellIndex)))
{
SelectedCells.Add(new Tuple<double, double>(Args.RowIndex, Args.CellIndex));
}
}
public async Task Select()
{
foreach (var cell in SelectedCells)
{
var column = GridInstance.Columns[Convert.ToInt32(cell.Item2)].Field;
await GridInstance.UpdateCellAsync(cell.Item1, column, "updated");
}
}
|
Thank you for your reply. But I'd to say sorry that your solution are not correct. It just keep append the new selected cell to the selected cells but not remove the deselected cells.
I add below code to display the selected cells. please notice at the text below the update button.
```
<div>selected: @string.Join(", ", SelectedCells.Select(x => $"[{x.Item1}, {x.Item2}]"))</div>
```