Skip to content

Commit 1ea80ef

Browse files
Merge pull request #1217 from telerik/new-kb-updating-external-ui-after-commit-edit-net-maui-datagrid-02e821a0d6c24f699a1ff493f10ae3f8
Added new kb article updating-external-ui-after-commit-edit-net-maui-datagrid
2 parents b97a676 + 1e3ff08 commit 1ea80ef

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
---
2+
title: Updating External UI After Commit Edit Command is Executed in DataGrid for .NET MAUI
3+
description: Learn how to update external UI elements after committing edited values in the DataGrid for UI for .NET MAUI.
4+
type: how-to
5+
page_title: Displaying Updated Values in External UI After Commit Edit Command in .NET MAUI DataGrid
6+
meta_title: Displaying Updated Values in External UI After Commit Edit Command in .NET MAUI DataGrid
7+
slug: updating-external-ui-after-commit-edit-net-maui-datagrid
8+
tags: datagrid, ui-for-dotnet-maui, commit-edit-command, external-ui-update
9+
res_type: kb
10+
---
11+
12+
## Environment
13+
14+
| Version | Product | Author |
15+
| --- | --- | ---- |
16+
| 11.0.1 | DataGrid for .NET MAUI | [Dobrinka Yordanova](https://www.telerik.com/blogs/author/dobrinka-yordanova) |
17+
18+
## Description
19+
20+
I want to display calculated values from two edited DataGrid cells in an external UI element, such as a `Label`. I use the `CommitEditCommand` in the UI for .NET MAUI DataGrid to run the calculation after editing the cells. However, the `CommitEditCommand` does not seem to have the updated cell values.
21+
22+
This knowledge base article also answers the following questions:
23+
- How to use `CommitEditCommand` to update an external UI component in .NET MAUI DataGrid?
24+
- How to bind external UI elements to calculated cell values in .NET MAUI DataGrid?
25+
- How to handle updates after cell edits in .NET MAUI DataGrid?
26+
27+
## Solution
28+
29+
To successfully update external UI elements with calculated values after editing DataGrid cells, follow these steps:
30+
31+
**1.** Define the `CommitEditCommand` to perform custom logic after committing the edited values of the DataGrid cells. Use the `DataGridCommandId.CommitEdit` to invoke the default commit functionality.
32+
33+
```csharp
34+
public class CommitEditCommand : DataGridCommand
35+
{
36+
private readonly Data _dataContext;
37+
private readonly Label _externalLabel;
38+
39+
public CommitEditCommand(Data data, Label label)
40+
{
41+
_dataContext = data;
42+
_externalLabel = label;
43+
this.Id = DataGridCommandId.CommitEdit;
44+
}
45+
46+
public override void Execute(object parameter)
47+
{
48+
var context = (EditContext)parameter;
49+
this.Owner.CommandService.ExecuteDefaultCommand(DataGridCommandId.CommitEdit, context);
50+
51+
// Update external UI element after committing the values.
52+
double total = _dataContext.Totale;
53+
_externalLabel.Text = "Importo Totale: " + total.ToString();
54+
}
55+
}
56+
```
57+
58+
**2.** Bind the external UI element (e.g., `Label`) to the calculated value after updating it in the `CommitEditCommand` logic.
59+
60+
```xaml
61+
<Grid RowDefinitions="*,auto">
62+
<telerik:RadDataGrid ItemsSource="{Binding Items}" UserEditMode="Cell"
63+
ListenForNestedPropertyChange="True" x:Name="datagrid" AutoGenerateColumns="False">
64+
<telerik:RadDataGrid.Columns>
65+
<telerik:DataGridNumericalColumn PropertyName="Descrizione" />
66+
<telerik:DataGridNumericalColumn PropertyName="Importo" />
67+
</telerik:RadDataGrid.Columns>
68+
</telerik:RadDataGrid>
69+
70+
<VerticalStackLayout Grid.Row="1">
71+
<Label BackgroundColor="LightBlue" HorizontalOptions="Start"
72+
x:Name="label" FontSize="22" Padding="8" Margin="8" WidthRequest="100" />
73+
</VerticalStackLayout>
74+
</Grid>
75+
```
76+
77+
**3.** Add sample `Data` model and `ViewModel`
78+
79+
```csharp
80+
public class Data : NotifyPropertyChangedBase
81+
{
82+
private double _descrizione;
83+
private double _importo;
84+
85+
public double Descrizione
86+
{
87+
get => _descrizione;
88+
set => this.UpdateValue(ref _descrizione, value);
89+
}
90+
91+
public double Importo
92+
{
93+
get => _importo;
94+
set => this.UpdateValue(ref this._importo, value);
95+
}
96+
97+
public double Totale => this.Importo * this.Descrizione;
98+
}
99+
100+
public class ViewModel : NotifyPropertyChangedBase
101+
{
102+
public ObservableCollection<Data> Items { get; }
103+
public Data Item { get; set; }
104+
105+
public ViewModel()
106+
{
107+
Items = new ObservableCollection<Data>
108+
{
109+
new Data { Importo = 0.0, Descrizione = 1 },
110+
};
111+
Item = Items[0];
112+
}
113+
}
114+
```
115+
116+
**4.** In page's code-behind:
117+
118+
* Set the `ViewModel`.
119+
* Add the `CommitEditCommand` to the DataGrid `Commands` collection.
120+
121+
```csharp
122+
public partial class MainPage : ContentPage
123+
{
124+
private readonly ViewModel _viewModel;
125+
126+
public MainPage()
127+
{
128+
InitializeComponent();
129+
_viewModel = new ViewModel();
130+
this.BindingContext = _viewModel;
131+
132+
var item = _viewModel.Items[0];
133+
datagrid.Commands.Add(new CommitEditCommand(item, label));
134+
label.Text = "Importo Totale: " + item.Totale.ToString();
135+
}
136+
}
137+
```
138+
139+
## See Also
140+
141+
- [DataGrid Overview](https://docs.telerik.com/devtools/maui/controls/datagrid/overview)
142+
- [DataGrid Commands Documentation](https://docs.telerik.com/devtools/maui/controls/datagrid/commands/overview)

0 commit comments

Comments
 (0)