Row in ASP.NET CORE Grid Control
21 Dec 20228 minutes to read
The row represents record details fetched from data source.
Row Customization
Using event
You can customize the appearance of a row by using the rowDataBound
event. The rowDataBound
event triggers for every row. In the event handler, you can get the rowDataBoundEventArgs that contains details of the row.
<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" enableHover="false" allowSelection="false" rowDataBound="rowBound">
<e-grid-pagesettings pageSize="6"></e-grid-pagesettings>
<e-grid-columns>
<e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="100"></e-grid-column>
<e-grid-column field="CustomerID" headerText="Customer ID" width="120"></e-grid-column>
<e-grid-column field="ShipCity" headerText="Ship City" width="100"></e-grid-column>
<e-grid-column field="Freight" headerText="Freight" width="100" format='C2'></e-grid-column>
<e-grid-column field="ShipName" headerText="Ship Name" width="100"></e-grid-column>
</e-grid-columns>
</ejs-grid>
<script>
function rowBound(args) {
if (args.data['Freight'] < 10) {
args.row.classList.add('below-30');
} else if (args.data['Freight'] < 80) {
args.row.classList.add('below-80');
} else {
args.row.classList.add('above-80');
}
}
</script>
public IActionResult Index()
{
ViewBag.DataSource = OrderDetails.GetAllRecords();
return View();
}
Using CSS customize alternate rows
You can change the grid’s alternative rows’ background color by overriding the .e-altrow class.
.e-grid .e-altrow {
background-color: #fafafa;
}
Refer to the following example.
<ejs-grid id="Grid" dataSource="@ViewBag.DataSource">
<e-grid-columns>
<e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="120"></e-grid-column>
<e-grid-column field="CustomerID" headerText="Customer ID" width="140"></e-grid-column>
<e-grid-column field="Freight" headerText="Freight" width="120" format='C2'></e-grid-column>
<e-grid-column field="OrderDate" headerText="Order Date" format="yMd" width="140"></e-grid-column>
</e-grid-columns>
</ejs-grid>
<style>
.e-grid .e-altrow {
background-color: #fafafa;
}
</style>
public IActionResult Index()
{
ViewBag.DataSource = OrderDetails.GetAllRecords();
return View();
}
Using CSS customize selected row
The background color of the selected row can be changed by overriding the following CSS style.
.e-grid td.e-active {
background-color: #f9920b;
}
This is demonstrated in the following sample:
<ejs-grid id="Grid" dataSource="@ViewBag.DataSource">
<e-grid-columns>
<e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="120"></e-grid-column>
<e-grid-column field="CustomerID" headerText="Customer ID" width="140"></e-grid-column>
<e-grid-column field="Freight" headerText="Freight" width="120" format='C2'></e-grid-column>
<e-grid-column field="OrderDate" headerText="Order Date" format="yMd" width="140"></e-grid-column>
</e-grid-columns>
</ejs-grid>
<style>
.e-grid td.e-active {
background-color: #f9920b;
}
</style>
public IActionResult Index()
{
var orders = OrderDetails.GetAllRecords();
ViewBag.datasource = orders;
return View();
}
Adding a new row programmatically
The Grid can add a new row between the existing rows using the addRecord
method of the Grid.
This is demonstrated in the following sample:
<ejs-button id="add" content="Add Row" isPrimary="true"></ejs-button>
<ejs-grid id="Grid" dataSource="@ViewBag.DataSource">
<e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true"></e-grid-editSettings>
<e-grid-columns>
<e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="120" isPrimaryKey="true"></e-grid-column>
<e-grid-column field="CustomerID" headerText="Customer ID" width="140"></e-grid-column>
<e-grid-column field="Freight" headerText="Freight" width="120" format='C2'></e-grid-column>
<e-grid-column field="ShipName" headerText="Ship Name" width="140"></e-grid-column>
</e-grid-columns>
</ejs-grid>
<script>
document.getElementById('add').onclick = function () {
document.getElementById('Grid').addRecord(
{ OrderID: 3232, CustomerID: 'ALKIT', Freight: 40, ShipName: 'Que Delícia'}, 2);
}
</script>
public IActionResult Index()
{
var orders = OrderDetails.GetAllRecords();
ViewBag.datasource = orders;
return View();
}
NOTE
When working with remote data, it is impossible to add a new row between the existing rows.
How to get the row information when hovering over the cell
It is possible to get the row information when hovering over the specific cell. This can be achieved by using the rowDataBound event and getRowInfo
method of the Grid.
In the following sample, the mouseover
event is bound to a grid row in the rowDataBound
event, and when hovering over the specific cell, using the getRowInfo
method, row information will be retrieved and displayed in the console.
<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" rowDataBound="rowDataBound">
<e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true"></e-grid-editSettings>
<e-grid-columns>
<e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="120"></e-grid-column>
<e-grid-column field="CustomerID" headerText="Customer ID" width="140"></e-grid-column>
<e-grid-column field="Freight" headerText="Freight" width="120" format='C2'></e-grid-column>
<e-grid-column field="OrderDate" headerText="Order Date" format="yMd" width="140"></e-grid-column>
</e-grid-columns>
</ejs-grid>
<script>
function rowDataBound(args) {
var gridElement = document.getElementById('Grid').ej2_instances[0];
args.row.addEventListener('mouseover', function (e) {
console.log(gridElement.getRowInfo(e.target))
})
}
</script>
public IActionResult Index()
{
ViewBag.DataSource = OrderDetails.GetAllRecords();
return View();
}