Skip to content

Commit b90805f

Browse files
Merge pull request #1197 from telerik/new-kb-retrieve-column-row-index-cell-tap-maui-datagrid-cd0c3172e0b14b70a0be8c4b52b7ce63
Added new kb article retrieve-column-row-index-cell-tap-maui-datagrid
2 parents ae9ab1d + cc20faa commit b90805f

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
---
2+
title: Getting Column and Row Index Using Cell Tap Command in DataGrid for MAUI
3+
description: Learn how to retrieve the column index and row index when using the Cell Tap command in the DataGrid for MAUI.
4+
type: how-to
5+
page_title: Retrieve Column and Row Index with Cell Tap Command in MAUI DataGrid
6+
meta_title: Retrieve Column and Row Index with Cell Tap Command in MAUI DataGrid
7+
slug: retrieve-column-row-index-cell-tap-maui-datagrid
8+
tags: datagrid,maui,column,row,index,cell-tap
9+
res_type: kb
10+
---
11+
12+
## Environment
13+
14+
| Version | Product | Author |
15+
| --- | --- | ---- |
16+
| 11.0.1 | Telerik UI for .NET MAUI DataGrid | [Dobrinka Yordanova](https://www.telerik.com/blogs/author/dobrinka-yordanova) |
17+
18+
## Description
19+
20+
I want to retrieve the column index and row index by using the Cell Tap command in the [DataGrid for MAUI]({%slug datagrid-overview%}).
21+
22+
This knowledge base article also answers the following questions:
23+
- How to determine tapped cell row and column index in MAUI DataGrid?
24+
- How to use `DataGridCellInfo` to get row and column index in MAUI DataGrid?
25+
- How to implement a custom `CellTap` command for MAUI DataGrid?
26+
27+
## Solution
28+
29+
To achieve this, use the `GetDataView` method of the DataGrid to access the sorted, filtered, and grouped view of the items. The `DataGridCellInfo` parameter provides information about the tapped cell's value, column, and row item. Combine these approaches to calculate the row index.
30+
31+
Follow these steps:
32+
33+
1. Implement a custom `DataGridCommand` to handle the Cell Tap event.
34+
2. Access the `DataGridCellInfo` parameter to retrieve the value, column, and row item.
35+
3. Use the `GetDataView` method to get the DataGrid's view of the data.
36+
4. Retrieve the row index using the `IndexOf` method.
37+
5. Optionally, you can display a message or take further actions based on this data.
38+
39+
Here's an example implementation:
40+
41+
```csharp
42+
public class CellTapUserCommand : DataGridCommand
43+
{
44+
public CellTapUserCommand()
45+
{
46+
Id = DataGridCommandId.CellTap;
47+
}
48+
49+
public override bool CanExecute(object parameter)
50+
{
51+
return true;
52+
}
53+
54+
public override void Execute(object parameter)
55+
{
56+
if (parameter is not DataGridCellInfo context)
57+
return;
58+
59+
var cellValue = context.Value; // The value of the tapped cell
60+
var cellColumn = context.Column; // The column of the tapped cell
61+
var rowValue = context.Item; // The row item
62+
63+
// Get the DataView of the DataGrid
64+
var dv = cellColumn.DataGrid.GetDataView();
65+
66+
int rowIndex = 0;
67+
if (dv.Items is IList list)
68+
{
69+
rowIndex = list.IndexOf(context.Item); // Get the row index
70+
}
71+
else
72+
{
73+
// Fallback: handle grouping or convert to list
74+
var itemsList = dv.Items.ToList();
75+
rowIndex = itemsList.IndexOf(rowValue);
76+
}
77+
78+
var message = $"You tapped on {cellValue} inside {cellColumn.HeaderText} column, which is row index {rowIndex}!";
79+
80+
Debug.WriteLine(message);
81+
82+
// Optionally display a message
83+
App.Current?.MainPage?.DisplayAlert("CellTap Command:", message, "OK");
84+
85+
// Execute the default command behavior
86+
this.Owner.CommandService.ExecuteDefaultCommand(DataGridCommandId.CellTap, parameter);
87+
}
88+
}
89+
```
90+
91+
## See Also
92+
93+
- [DataGrid Overview for MAUI](https://docs.telerik.com/devtools/maui/controls/datagrid/overview)
94+
- [DataGrid Commands for MAUI](https://docs.telerik.com/devtools/maui/controls/datagrid/commands/overview)

0 commit comments

Comments
 (0)