How to get selected row indexes in Grid?
Solution:
You can get the selected row indexes by using the selectedRowsIndexes property in Grid. The following code example shows how to get the selected row indexes in Grid. 1. Render the Grid control.
JS
<div id="Grid"></div> <script type="text/javascript"> $(function () {// Document is ready. $("#Grid").ejGrid({ dataSource: window.gridData, allowPaging: true, allowSelection: true, selectionType: "multiple", columns: [ { field: "OrderID", headerText: "Order ID, width: 100}, { field: "CustomerID", headerText: "Customer ID", width: 100 }, { field: "Freight", headerText: "Freight", width: 100, format: "{0:C}" }, { field: "ShipCountry", headerText: "Ship Country", width: 100 }, ], rowSelected: "rowSelected", }); }); </script>
MVC
[In View] @(Html.EJ().Grid<object>("Grid") .Datasource((IEnumerable<object>)ViewBag.data)) .AllowPaging() .AllowSelection() .SelectionType(SelectionType.Multiple) }) .Columns(col => { col.Field("OrderID").HeaderText("Order ID").Width(100).Add(); col.Field("CustomerID").HeaderText("Customer ID").Width(100).Add(); col.Field("Freight").Width(100).Format("{0:C").Add(); col.Field("ShipCountry").HeaderText("Ship Country").Width(100).Add(); }) .ClientSideEvents(eve=>eve.rowSelected ("rowSelected")) )
[Controller] namespace EJGrid.Controllers { public class HomeController : Controller { public ActionResult Index() { var DataSource = OrderRepository.GetAllRecords(); ViewBag.data = DataSource; return View(); } } }
ASP.NET
[aspx] <ej:Grid ID="Grid" runat="server" AllowPaging="True" AllowSelection="True" SelectionType="Multiple" > <ClientSideEvents rowSelected="rowSelected" /> <Columns> <ej:Column Field="OrderID" HeaderText="Order ID" Width="100"/> <ej:Column Field="CustomerID" HeaderText="Customer ID" Width="100"/> <ej:Column Field="Freight" HeaderText="Freight" Format="{0:C}" Width="100"/> <ej:Column Field="ShipCountry" HeaderText="Ship Country" Width="100"/> </Columns> </ej:Grid>
[CS] public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { this.Grid.DataSource = new NorthwindDataContext().Orders. ToList(); this.Grid.DataBind(); } }
2. You can get the selectedRowsIndexes in rowSelected event, which triggered after a row is selected and get the selected records by using getSelectedRecords method
JS
function rowSelected(args) { var selectedrowindex = this.selectedRowsIndexes; // get selected row indexes alert(selectedrowindex); var selectedrecords = this.getSelectedRecords(); // get selected records }
The following output is displayed as a result of the above code example.
Figure: Select the rowIndexes by rowSelected Event
I hope you enjoyed learning about how to get selected row indexes in Grid.
You can refer to our JavaScript Grid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our JavaScript Grid example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!