|
<SfButton OnClick="Show" CssClass="e-primary" Content="Show"></SfButton>
<SfGrid @ref="DefaultGrid" AllowPaging="true" DataSource="@Orders" AllowSorting="true" AllowSelection="true"
AllowFiltering="true">
<GridEvents RowSelected="RowSelectHandler" RowDeselected="RowDeselectHandler" TValue="Order"></GridEvents>
<GridEditSettings AllowEditing="true" AllowDeleting="true" AllowAdding="true"></GridEditSettings>
<GridSelectionSettings EnableToggle="true" PersistSelection="true"></GridSelectionSettings>
<GridColumns>
. . .
</GridColumns>
</SfGrid>
@code{
SfGrid<Order> DefaultGrid;
. . .
public async void Show()
{
// get all the selected records
var selectedRec = await DefaultGrid.GetSelectedRecords();
}
. . .
} |
|
<SfGrid DataSource="@OrderData">
<GridColumns>
<GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" TextAlign="TextAlign.Center" Width="120"></GridColumn>
<GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer ID" TextAlign="TextAlign.Center" Width="130"></GridColumn>
<GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" TextAlign="TextAlign.Center" Width="130"></GridColumn>
<GridColumn Field=@nameof(Order.ShipName) HeaderText="Ship Name" TextAlign="TextAlign.Center" Width="120"></GridColumn>
<GridColumn Field=@nameof(Order.Verified) HeaderText="Verified" TextAlign="TextAlign.Center" DisplayAsCheckBox="true" Width="120"></GridColumn>
</GridColumns>
</SfGrid>
@code{
. . .
} |
Sometimes it just takes asking the question in the right way. Thanks, Rahul! Another customer benefitted from your answer.
Next question - how do we make that checkbox column we show interactive, so that clicking it will trigger either the RowSelected or RowDeselected events? Or even trigger a custom event?
Hi Team,
Based on your query, we understand that you would like to improve the
responsiveness of the checkbox for selection operations.We are happy to inform
you that you can achieve this by utilizing the checkbox click to trigger both
the RowSelected and RowDeselected events. We have provided a sample for your
reference, which you can find attached to this response.
If you have any further queries, please get back to us.
Regards,
Sarvesh
Hi Sarveswaran,
I had a look at your sample project in SfGridSelection_dfd4aa84.zip above, and have 2 questions relating to it:
I need to be able to do all of the following:
I think if you added the ability to bind a GridColumn of Type="Column.CheckBox" to a property on the model, it would be the solution for both my questions, something like:
Thank you for your assistance
Hi Pierre,
Greetings from Syncfusion support.
Query - How
do I get the complete list of checked orders?
We suggest you to use our inbuilt GetSelectedRecords method to get the
collection of the selected records from the grid. Refer the attached API link
for your reference.
Reference: https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.SfGrid-1.html#Syncfusion_Blazor_Grids_SfGrid_1_GetSelectedRecordsAsync
Query: How would
I, for example, check the first 5 orders on startup?
We have validated your and you want to preselect the rows at initial rendering of the Grid. You can achieve your requirement by using DataBound event and SelectRows method of the Grid. Here, we have selected row index’s(0,1,2,3,4) at initial rendering of the Grid. Find the below code snippets and sample for your reference.
|
<SfGrid @ref="Grid" DataSource="@Orders" AllowSelection="true" AllowFiltering="true" AllowPaging="true"> <GridEvents DataBound="Data” RowSelected="RowSelectHandler" TValue="Order"></GridEvents> . . </SfGrid>
@code {
SfGrid<Order> Grid; public List<Order> Orders { get; set; } int[] index = { 0, 1, 2, 3, 4 };
public async Task RowSelectHandler(RowSelectEventArgs<Order> args) { SelectedRowIndexes = await this.Grid.GetSelectedRowIndexes(); selected = await this.Grid.GetSelectedRecordsAsync(); txtSelectedRecordCount = selected.Count() + " rows selected";
StateHasChanged(); }
public async Task Data(object args) { await Grid.SelectRows(index); } } |
Kindly refer the modified sample for your reference. Please
get back to us, if you have any further queries.
Regards,
Sarvesh
Hi Sarvesh,
I apologise for my delayed response. Thank you for your solutions to my queries. Your solution to my second query does indeed check the first 5 items on startup. The problem is that when you page to any page, the first 5 items on that page always stays checked, regardless of any items that you checked/unchecked (changes are therefore ignored and first 5 items on the page are always checked). Looking back, I don't think I worded my query correctly, so here is what I need to do (in your example):
On startup, I need to be able to specify which items are checked. The checked items could be spread across all the pages. So, say the following orderIDs need to be checked: 1001, 1002, 1003, 1020,1024 and 1029. On startup the first page is shown (containing orders 1001 to 1012) so therfore orders 1001, 1002 and 1003 need to be checked. If the user pages to page 2 (containing orders 1013 to 1024), orders 1020 and 1024 must be checked. If the user pages to page 3 (containing orders 1025 to 1036), orders 1029 must be checked. Any changes the user make must then be preserved. For example, if the user is on page 1 and decides to uncheck order 1003 and check order 1006, when he goes to page 2, 1020 and 1024 will be checked. If he now goes back to page 1 again, orders 1001, 1002 and 1006 will be checked.
As mentioned originally, i think the easiest solution would be to add the ability to bind the checkbox column to a property on the model, something like this (I have added Checked property):
public class Order
{
public int? OrderID { get; set; }
public string CustomerID { get; set; }
public DateTime? OrderDate { get; set; }
public double? Freight { get; set; }
public bool Checked { get; set; }
}
<SfGrid @ref="Grid" DataSource="@Orders" AllowSelection="true" AllowFiltering="true" AllowPaging="true">
<GridSelectionSettings CheckboxOnly="true" PersistSelection="true" Type="Syncfusion.Blazor.Grids.SelectionType.Multiple"></GridSelectionSettings>
<GridEvents OnActionBegin="ActionBeginHandler" DataBound="Data" RowSelected=" RowSelectHandler" RowDeselected="RowDeselectHandler" TValue="Order"></GridEvents>
<GridFilterSettings Type="Syncfusion.Blazor.Grids.FilterType.Menu"></GridFilterSettings>
<GridColumns>
<GridColumn Field=@nameof(Order.Checked) Type="ColumnType.CheckBox" Width="30"></GridColumn>
<GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" TextAlign="TextAlign.Right" Width="120"></GridColumn>
<GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="150"></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>
I'm not sure how easy that would be to implement on your side
Hi Pierre,
Based on your query, we have prepared a sample that selects the records in the
initial loading using the index of the row obtained from the GetRowIndexByPrimaryKey
method in the DataBound event. Additionally, when the user deselects a
record, we remove the specific index from the list using the Remove
method in the RowDeselectHandler event. We have attached the sample code
for your reference.
|
List<int> index = new List<int> { 1001, 1002, 1012, 1006, 1024 };
public async Task Data(object args) { foreach(int num in index) { var val = Grid.GetRowIndexByPrimaryKeyAsync(num); int value1 = Convert.ToInt32(val.Result); await Grid.SelectRowAsync(value1); } }
public void RowDeselectHandler(RowDeselectEventArgs<Order> args) { var data = args.Data.OrderID; index.Remove(data); } |
Reference: https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.SfGrid-1.html#Syncfusion_Blazor_Grids_SfGrid_1_GetRowIndexByPrimaryKeyAsync_System_Object_
https://blazor.syncfusion.com/documentation/datagrid/events#databound
https://blazor.syncfusion.com/documentation/datagrid/events#rowdeselected
Please get back to us if you have any further queries.
Regards,
Sarvesh
Hi Sarvesh,
The example you provided works, thank you so much. I will implement it in my project and let you know if there are any further issues.
Keep up the great service.
Regards
Pierre
Thanks for an update.
We'll currently close this ticket. Please get back to us, if you have any further queries.
Regards,
Sarvesh