Select grid rows based on certain condition

17 Feb 20222 minutes to read

You can select the specific row in the grid based on a certain condition by using the selectRows method in the dataBound event of Grid.

In the below demo, we have selected the grid rows only when EmployeeID column value greater than 3.

<div id="GridParent">
    <ejs-grid id="Grid" dataSource="@ViewBag.DataSource" height="315" allowSelection="true" allowPaging="true" dataBound="dataBound" rowDataBound="rowDataBound">
        <e-grid-selectionsettings type="Multiple"></e-grid-selectionsettings>
        <e-grid-columns>
            <e-grid-column field="OrderID" headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="100"></e-grid-column>
            <e-grid-column field="CustomerID" headerText="Customer ID" type="string" width="120"></e-grid-column>
            <e-grid-column field="EmployeeID" headerText="Employee ID" textAlign="Right" width="120"></e-grid-column>
            <e-grid-column field="ShipCity" headerText="Ship City" width="120"></e-grid-column>
            <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>
        </e-grid-columns>
    </ejs-grid>
</div>

<script>

    var selIndex = [];

    function rowDataBound(args) {
        if (args.data['EmployeeID'] > 3) {
            selIndex.push(parseInt(args.row.getAttribute('aria-rowindex')));
        }
    }

    function dataBound(args) {
        if (selIndex.length) {
            this.selectRows(selIndex);
            selIndex = [];
        }
    }
</script>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}