How to select a row at NOT initial rendering.

Hello.

I'm not very good at English, but I'll do my best to communicate.

In the docs it's described how to select rows at initial rendering.


Is there any way to select rows at NOT initial rendering?

For example, if it is already rendered grid, and select a condition and submit in an external form, the corresponding row(s) will be selected.

Regards, 

Atsushi


5 Replies

JP Jeevakanth Palaniappan Syncfusion Team July 14, 2021 06:50 AM UTC

Hi Atsushi, 
 
Greetings from Syncfusion support. 
 
We have checked your query but we are quite unclear about your exact requirement. We suspect that you want to perform selection manually by using a button click. If so you can call the SelectRow or SelectRows method in the button click handler. 
 
await Grid.SelectRow(RowIndex); //RowIndex is double type 
await Grid.SelectRow(RowIndexes); //RowIndexes is double[] type 
 
If this is not your scenario then kindly share us more information on your requirement. 
 
  1. Do you want to select a row or multiple row?
  2. Do you want to select the row/rows manually/programmatically by using the grid methods?
  3. Share us your requirement in detail.
  4. Share us any screenshot or video demo showing our requirement.
 
The above requested details will be helpful for us to validate your problem and to provide you with a better solution as early as possible. 
 
Regards, 
Jeevakanth SP. 
 



AS Atsushi Sugimoto July 15, 2021 06:13 AM UTC

Hi Jeevakanth,


Thanks for the reply.


As you told me, the simple Selectrow(s) method worked correctly.

I hadn't noticed it either, but the situation where it didn't work was updating the DataSource just before.


I wrote a sample code based on the code in the following URL.

https://blazor.syncfusion.com/documentation/datagrid/selection/#multiple-selection-based-on-condition


@page "/selectrow"

@using Syncfusion.Blazor.Grids


<h3>SelectRowTest</h3>


<SfGrid @ref="@Grid" DataSource="@Orders" AllowFiltering="true" AllowPaging="true" Height="315">
    <GridSelectionSettings Type="SelectionType.Multiple"></GridSelectionSettings>
    <GridColumns>
        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Center" Width="120"></GridColumn>
        <GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer ID" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Center" Width="150"></GridColumn>
        <GridColumn Field=@nameof(Order.OrderDate) HeaderText=" Order Date" Format="d" Type="ColumnType.Date" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Center" Width="130"></GridColumn>
        <GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Center" Width="120"></GridColumn>
    </GridColumns>
</SfGrid>
<label for="target-customer-id">Target customer ID</label>
<input type="text" id="target-customer-id" @bind="TargetCustomerID"/>
<button @onclick="ResetOrders">Reset Orders</button>


@code{
    SfGrid<Order> Grid;
    public List<Order> Orders { get; set; }
    List<double> SelectIndex { get; set; }
    string TargetCustomerID = "ALFKI";


    protected override void OnInitialized()
    {
        InitOrders();
    }


    private void InitOrders()
    {
        Orders = Enumerable.Range(1, 12).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 async Task ResetOrders()
    {
        InitOrders();
        var IndexNum = 0;
        SelectIndex = new List<double>();
        foreach (var record in Orders)
        {
            if (record.CustomerID == TargetCustomerID)
            {
                SelectIndex.Add(IndexNum);
            }
            IndexNum++;
        }
        await Grid.SelectRows(SelectIndex.ToArray());
    }


    public class Order
    {
        public int? OrderID { get; set; }
        public string CustomerID { get; set; }
        public DateTime? OrderDate { get; set; }
        public double? Freight { get; set; }
    }
}


What should I do if I want to update the DataSource and make the selection programmatically at the same time?


Thank you for any help you can provide.



JP Jeevakanth Palaniappan Syncfusion Team July 16, 2021 06:17 AM UTC

Hi Atsushi, 
 
Thanks for sharing the full code snippet. We have validated your query and we suggest you to select the rows in the DataBound event of the grid. Please refer the below code snippet and the sample for your reference. 
 
<SfGrid @ref="@Grid" DataSource="@Orders" AllowFiltering="true" AllowPaging="true" Height="315"> 
    <GridSelectionSettings Type="SelectionType.Multiple"></GridSelectionSettings> 
    <GridEvents TValue="Order" DataBound="DataBoundHandler"></GridEvents> 
    <GridColumns> 
        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Center" Width="120"></GridColumn> 
        <GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer ID" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Center" Width="150"></GridColumn> 
        <GridColumn Field=@nameof(Order.OrderDate) HeaderText=" Order Date" Format="d" Type="ColumnType.Date" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Center" Width="130"></GridColumn> 
        <GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Center" Width="120"></GridColumn> 
    </GridColumns> 
</SfGrid> 
<label for="target-customer-id">Target customer ID</label> 
<input type="text" id="target-customer-id" @bind="TargetCustomerID" /> 
<button @onclick="ResetOrders">Reset Orders</button> 
 
 
@code{ 
    SfGrid<Order> Grid; 
    public List<Order> Orders { get; set; } 
    List<double> SelectIndex { get; set; } 
    string TargetCustomerID = "ALFKI"; 
 
    bool SelectRows = false; 
 
 
    protected override void OnInitialized() 
    { 
        InitOrders(); 
    } 
 
 
    private void InitOrders() 
    { 
        Orders = Enumerable.Range(1, 12).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 async Task ResetOrders() 
    { 
        InitOrders(); 
        SelectRows = true; 
    } 
 
    public async Task DataBoundHandler() 
    { 
        if (SelectRows)  
        { 
            SelectRows = false; 
            var IndexNum = 0; 
            SelectIndex = new List<double>(); 
            foreach (var record in Orders) 
            { 
                if (record.CustomerID == TargetCustomerID) 
                { 
                    SelectIndex.Add(IndexNum); 
                } 
                IndexNum++; 
            } 
            await Grid.SelectRows(SelectIndex.ToArray()); 
        } 
    } 
 
 
    public class Order 
    { 
        public int? OrderID { get; set; } 
        public string CustomerID { get; set; } 
        public DateTime? OrderDate { get; set; } 
        public double? Freight { get; set; } 
    } 
} 


Please get back to us if you have any other queries. 

Regards, 
Jeevakanth SP. 



AS Atsushi Sugimoto July 16, 2021 11:27 AM UTC

Hi  Jeevakanth, 


I did as you taught me and it worked perfectly.

I'll be using DataBound from now on.

Thank you for your support.


Atsushi.



JP Jeevakanth Palaniappan Syncfusion Team July 19, 2021 05:35 AM UTC

Hi Atsushi, 
 
You are most welcome. Please get back to us if you have any other queries. 
 
Regards, 
Jeevakanth SP. 


Loader.
Up arrow icon