Skip to content

Commit fcc8c0e

Browse files
Merge pull request #1195 from telerik/new-kb-force-commit-command-maui-datagrid-outside-tap-a69b882bb8ab4f1eb4e7d82605bb34a4
Added new kb article force-commit-command-maui-datagrid-outside-tap
2 parents 3317142 + d9fb000 commit fcc8c0e

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
---
2+
title: Forcing Commit Command When Tapping Outside the DataGrid in MAUI
3+
description: Learn how to force the CommitEdit command in the DataGrid for MAUI when tapping outside the control or interacting with another grid.
4+
type: how-to
5+
page_title: Triggering CommitEdit in MAUI DataGrid on External UI Tap or Second DataGrid Interaction
6+
meta_title: Triggering CommitEdit in MAUI DataGrid on External UI Tap or Second DataGrid Interaction
7+
slug: force-commit-command-maui-datagrid-outside-tap
8+
tags: datagrid,maui,commitedit,celltap,tapgesture,programmatic
9+
res_type: kb
10+
---
11+
12+
## Environment
13+
14+
| Version | Product | Author |
15+
| --- | --- | ---- |
16+
| 11.0.0 | Telerik UI for .NET MAUI DataGrid | [Dobrinka Yordanova](https://www.telerik.com/blogs/author/dobrinka-yordanova) |
17+
18+
## Description
19+
20+
I need a way to force the first DataGrid in MAUI to commit edits when interacting with external UI elements or a second DataGrid in the same page. Specifically, when editing a cell in the first grid and tapping on another grid or external UI, the edit should be committed programmatically.
21+
22+
This knowledge base article also answers the following questions:
23+
- How to force `CommitEdit` in MAUI DataGrid when tapping outside the grid?
24+
- How to use `CommitEdit` with multiple MAUI DataGrids?
25+
- How to programmatically commit edits in MAUI DataGrid?
26+
27+
## Solution
28+
29+
To force the `CommitEdit` command when tapping outside the MAUI DataGrid or another grid, follow one of the solutions suggested below.
30+
31+
### Solution 1: Tapping on External UI
32+
33+
Use a `TapGestureRecognizer` on an external UI element to programmatically execute the `CommitEdit` command.
34+
35+
**1.** The DataGrid definition in XAML:
36+
37+
```xaml
38+
<Grid RowDefinitions="auto,*">
39+
<Grid BackgroundColor="LightBlue" HeightRequest="200">
40+
<Grid.GestureRecognizers>
41+
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" />
42+
</Grid.GestureRecognizers>
43+
</Grid>
44+
45+
<telerik:RadDataGrid x:Name="grid"
46+
Grid.Row="1"
47+
ItemsSource="{Binding Clubs}"
48+
AutoGenerateColumns="False" />
49+
</Grid>
50+
```
51+
52+
**2.** Execute the `CommitEdit` command inside the gesture recognizer event handler:
53+
54+
```csharp
55+
private void TapGestureRecognizer_Tapped(object sender, TappedEventArgs e)
56+
{
57+
this.grid.CommandService.ExecuteCommand(DataGridCommandId.CommitEdit, new EditContext(null, ActionTrigger.Programmatic, null));
58+
}
59+
```
60+
61+
### Solution 2: Interaction with a Second DataGrid
62+
63+
Define a custom command for the second MAUI DataGrid's `CellTap`. This command will execute the `CommitEdit` command for the first grid.
64+
65+
**1.** Define the DataGrid in XAML:
66+
67+
```xaml
68+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
69+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
70+
xmlns:telerik="http://schemas.telerik.com/2022/xaml/maui"
71+
xmlns:local="clr-namespace:MauiApp13"
72+
Padding="20"
73+
x:Name="myPage"
74+
x:Class="MauiApp13.MainPage">
75+
<Grid RowDefinitions="*,*">
76+
<telerik:RadDataGrid x:Name="dataGrid" />
77+
<telerik:RadDataGrid x:Name="dataGrid1" Grid.Row="1" />
78+
</Grid>
79+
</ContentPage>
80+
```
81+
82+
**2.** Add a sample data and custom `CellTap` command definition. Inside the `Execute` method, execute the `CommitEdit` command of the first DataGrid:
83+
84+
```csharp
85+
public partial class MainPage : ContentPage
86+
{
87+
public MainPage()
88+
{
89+
InitializeComponent();
90+
91+
this.dataGrid.ItemsSource = new List<Data>
92+
{
93+
new Data { Country = "India", Capital = "New Delhi" },
94+
new Data { Country = "South Africa", Capital = "Cape Town" },
95+
};
96+
97+
this.dataGrid1.ItemsSource = new List<Data>
98+
{
99+
new Data { Country = "India", Capital = "New Delhi" },
100+
new Data { Country = "South Africa", Capital = "Cape Town" },
101+
new Data { Country = "Nigeria", Capital = "Abuja" },
102+
new Data { Country = "Singapore", Capital = "Singapore" }
103+
};
104+
105+
this.dataGrid1.Commands.Add(new CellTapUserCommand(this.myPage));
106+
}
107+
}
108+
109+
public class Data
110+
{
111+
public string Country { get; set; }
112+
public string Capital { get; set; }
113+
}
114+
115+
public class CellTapUserCommand : DataGridCommand
116+
{
117+
ContentPage myPage;
118+
119+
public CellTapUserCommand(ContentPage myPage)
120+
{
121+
Id = DataGridCommandId.CellTap;
122+
this.myPage = myPage;
123+
}
124+
125+
public override bool CanExecute(object parameter)
126+
{
127+
return true;
128+
}
129+
130+
public override void Execute(object parameter)
131+
{
132+
var context = parameter as DataGridCellInfo;
133+
var cellTap = $"You tapped on {context.Value} inside {context.Column.HeaderText} column \n";
134+
Application.Current.MainPage.DisplayAlert("CellTap Command: ", cellTap, "OK");
135+
this.Owner.CommandService.ExecuteDefaultCommand(DataGridCommandId.CellTap, parameter);
136+
137+
List<IVisualTreeElement> items = (List<IVisualTreeElement>)this.myPage.GetVisualTreeDescendants();
138+
foreach (IVisualTreeElement myControl in items)
139+
{
140+
if (myControl is RadDataGrid)
141+
{
142+
RadDataGrid control = (RadDataGrid)myControl;
143+
control.CommandService.ExecuteCommand(DataGridCommandId.CommitEdit, new EditContext(null, ActionTrigger.Programmatic, null));
144+
return;
145+
}
146+
}
147+
}
148+
}
149+
```
150+
151+
## See Also
152+
153+
- [DataGrid for MAUI Overview]({%slug datagrid-overview%})
154+
- [DataGrid Commands Documentation]({%slug datagrid-commands-overview%})
155+
- [MAUI Gesture Recognizers](https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/gestures/tap)

0 commit comments

Comments
 (0)