Programmatically update
Hello. How to implement the following scenario.
When you click the button, set the field values for the selected rows, and the selection should not be reset.
Initial state:
Desired result:
Thanks.
SIGN IN To post a reply.
7 Replies
1 reply marked as answer
JP
Jeevakanth Palaniappan
Syncfusion Team
November 23, 2020 02:56 PM UTC
Hi Constantine,
Greetings from Syncfusion support.
We have validated your query and you can set the PersistSelection of GridSelectionSettings as true to persist the selections and then you can use either SetCellValue or UpdateCell method to update the values. SetCellValue method updates the value in the datasource. UpdateCell method updates the value in the UI and update button will enabled to update the values in the datasource.
Please refer the below code snippet and the API link for your reference.
|
@using Syncfusion.Blazor.Grids
<Syncfusion.Blazor.Buttons.SfButton @onclick="SetCellValue">SetCellValue</Syncfusion.Blazor.Buttons.SfButton>
<Syncfusion.Blazor.Buttons.SfButton @onclick="UpdateCell">UpdateCell</Syncfusion.Blazor.Buttons.SfButton>
<SfGrid @ref="Grid" ID="Grid" DataSource="@Orders" AllowSorting="true" AllowPaging="true">
<GridPageSettings PageSize="8"></GridPageSettings>
<GridSelectionSettings PersistSelection="true"></GridSelectionSettings>
<GridEditSettings AllowEditing="true" AllowDeleting="true" AllowAdding="true" Mode="EditMode.Batch"></GridEditSettings>
<GridColumns>
<GridColumn Type="ColumnType.CheckBox" Width="50"></GridColumn>
<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 List<Order> Orders { get; set; }
SfGrid<Order> Grid { get; set; }
public void SetCellValue()
{
Grid.SetCellValue(1001, "CustomerID", "SetCellValue!");
}
private void UpdateCell()
{
Grid.UpdateCell(1, "CustomerID", "UpdateCell!");
}
protected override void OnInitialized()
{
Orders = Enumerable.Range(1, 75).Select(x => new Order()
{
OrderID = 1000 + x,
CustomerID = (new string[] { "ALFKI", "ANANTR", "ANTON", "BLONP", "BOLID" })[new Random().Next(5)],
Freight = 2.1 * x,
OrderDate = DateTime.Now.AddDays(-x),
}).ToList();
}
public class Order
{
public int? OrderID { get; set; }
public string CustomerID { get; set; }
public DateTime? OrderDate { get; set; }
public double? Freight { get; set; }
}
}
|
API Reference:
Please get back to us if you need further assistance.
Regards,
Jeevakanth SP.
Marked as answer
CO
Constantine
November 24, 2020 09:17 AM UTC
Thanks. PersistSelection is what was needed.
JP
Jeevakanth Palaniappan
Syncfusion Team
November 25, 2020 04:59 AM UTC
Hi Constantine,
Thanks for the update. Please get back to us if you have any other queries.
Regards,
Jeevakanth SP.
CO
Constantine
November 25, 2020 12:22 PM UTC
One remark
Changing the PersistSelection property also changes the row selection rules when clicking the top checkbox.
If the PersistSelection property is set to false, only records on the current page are selected.
If PersistSelection = true, records on all pages are selected. This is not at all obvious to users. In my opinion, this is unsafe because the user can change all records in the table without even knowing it. This behavior should, at very least, be able to be configure, right?
JP
Jeevakanth Palaniappan
Syncfusion Team
November 26, 2020 12:01 PM UTC
Hi Constantine,
On enabling persist selection, the selection will be maintained for all the grid actions and the selection will be maintained in all the pages which is the default behavior of the grid. For your requirement to update the records with the selection maintained, we recommend you to achieve it by using the UpdateCell method which changes the record values in the UI with selection maintained. Please refer the below code snippet to use the UpdateCell method
|
private void UpdateCell()
{
Grid.UpdateCell(1, "CustomerID", "UpdateCell!");
} |
Please get back to us if you have any other queries.
Regards,
Jeevakanth SP.
CO
Constantine
November 26, 2020 02:14 PM UTC
Okay thank you. I will try to ensure the safe use of this functionality.
JP
Jeevakanth Palaniappan
Syncfusion Team
November 27, 2020 04:51 AM UTC
Hi Constantine,
Thanks for the update. Get back to us if you have any other queries.
Regards,
Jeevakanth SP.
SIGN IN To post a reply.
- 7 Replies
- 2 Participants
- Marked answer
-
CO Constantine
- Nov 22, 2020 07:20 AM UTC
- Nov 27, 2020 04:51 AM UTC