How to get the RowIndex of a row in SfDataGrid when using DataGridTemplateColumn?
The .NET MAUI DataGrid provides various resolving methods to resolve the row index of grid rows based on certain criteria. The actual RowIndex of a row can be resolved by using the ResolveToRowIndex(recordRowIndex) method in SfDataGrid.
The RowIndex of a row can be obtained from DataGridTemplateColumn by retrieving the record index of the row using the bound data from its BindingContext and passing the recordRowIndex to the ResolveToRowIndex() method.
XAML
<syncfusion:DataGridTemplateColumn HeaderText="ID Number" MappingName="IDNumber">
<syncfusion:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Text="{Binding IDNumber}" WidthRequest="120" HeightRequest="40" Clicked="Button_Clicked"/>
</DataTemplate>
</syncfusion:DataGridTemplateColumn.CellTemplate>
</syncfusion:DataGridTemplateColumn>
C#
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void Button_Clicked(object sender, EventArgs e)
{
var button = sender as Button;
var record = button.BindingContext as Employee;
var recordRowIndex = viewModel.Employees.IndexOf(record);
var rowIndex = datagrid.ResolveToRowIndex(recordRowIndex);
}
}
Note: RowIndex of the row can also be accessed using CellTapped, CellDoubleTapped and CellLongPress events. When using a complex layout inside a Template, ensure to set the InputTransparent property of the views loaded in the DataTemplate of the DataGridTemplateColumn to True.
The following screenshot shows how to get the RowIndex of a row when using DataGridTemplateColumn in SfDataGrid.
Download the complete sample from GitHub
Conclusion
I hope you enjoyed learning how to get the RowIndex of a row when using DataGridTemplateColumn in SfDataGrid.
You can refer to our .NET MAUI DataGrid feature tour page to learn about its other groundbreaking feature representations. Explore our .NET MAUI DataGrid Documentation to understand how to present and manipulate data.
For current customers, check out our .NET MAUI components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our .NET MAUI DataGrid and other .NET MAUI components.
Please let us know in the comments section if you have any queries or require clarification. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!