Skip to content

Commit ea9d91c

Browse files
Merge pull request #1 from SyncfusionExamples/863267
863267 - How to get the cell value of specific row and column in .NET MAUI DataGrid (SfDataGrid).
2 parents 399a102 + 3e5ed67 commit ea9d91c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1635
-2
lines changed

CellValue.png

24.4 KB
Loading

README.md

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,85 @@
1-
# How-to-get-the-cell-value-of-specific-row-and-column-in-.NET-MAUI-DataGrid-SfDataGrid
2-
This demo shows how to get the cell value of specific row and column in .NET MAUI DataGrid SfDataGrid
1+
# How to get the cell value of specific row and column in .NET MAUI DataGrid SfDataGrid
2+
You can get the cell value of specific row and column in [SfDataGrid](https://www.syncfusion.com/maui-controls/maui-datagrid) by using the **SfDataGrid.GetCellValue** method. You need to pass the underlying data object and the mapping name of the column to find the cell value.
3+
4+
5+
```XML
6+
<StackLayout>
7+
<syncfusion:SfDataGrid ItemsSource="{Binding Employees}"
8+
AutoGenerateColumnsMode="None"
9+
ColumnWidthMode="Auto"
10+
DefaultColumnWidth="155">
11+
12+
<syncfusion:SfDataGrid.Columns>
13+
<syncfusion:DataGridTextColumn MappingName="EmployeeID"
14+
HeaderText="Employee ID" />
15+
<syncfusion:DataGridTextColumn MappingName="Name"
16+
HeaderText="Name" />
17+
<syncfusion:DataGridTextColumn MappingName="IDNumber"
18+
HeaderText="ID Number" />
19+
</syncfusion:SfDataGrid.Columns>
20+
21+
</syncfusion:SfDataGrid>
22+
23+
<Button Text="Click"
24+
HorizontalOptions="StartAndExpand"
25+
WidthRequest="460"
26+
TextColor="Black">
27+
<Button.Behaviors>
28+
<behaviors:ButtonBehavior />
29+
</Button.Behaviors>
30+
</Button>
31+
</StackLayout>
32+
```
33+
34+
The below code illustrates how to get the cell value of the specific row and column in a button click.
35+
36+
```XML
37+
public class ButtonBehavior : Behavior<Button>
38+
{
39+
protected override void OnAttachedTo(Button button)
40+
{
41+
base.OnAttachedTo(button);
42+
button.Clicked += OnButtonClicked!;
43+
}
44+
45+
protected override void OnDetachingFrom(Button button)
46+
{
47+
base.OnDetachingFrom(button);
48+
button.Clicked -= OnButtonClicked!;
49+
}
50+
51+
string? cellValue;
52+
private void OnButtonClicked(object sender, EventArgs e)
53+
{
54+
var button = sender as Button;
55+
var stackLayout = button!.Parent as StackLayout;
56+
var dataGrid = stackLayout!.Children[0] as SfDataGrid;
57+
foreach (var column in dataGrid!.Columns)
58+
{
59+
if (column.MappingName == "Name")
60+
{
61+
var rowData = dataGrid.GetRecordAtRowIndex(1);
62+
cellValue = dataGrid.GetCellValue(rowData, column.MappingName) as String;
63+
break;
64+
}
65+
}
66+
Application.Current!.MainPage!.DisplayAlert("ColumnName", cellValue, "Cancel");
67+
}
68+
}
69+
```
70+
71+
72+
![CellValue.png](CellValue.png)
73+
74+
[View sample in GitHub](https://github.com/SyncfusionExamples/How-to-get-the-cell-value-of-specific-row-and-column-in-.NET-MAUI-DataGrid-SfDataGrid)
75+
76+
Take a moment to explore this [documentation](https://help.syncfusion.com/maui/datagrid/overview), where you can find more information about Syncfusion .NET MAUI DataGrid (SfDataGrid) with code examples. Please refer to this [link](https://www.syncfusion.com/maui-controls/maui-datagrid) to learn about the essential features of Syncfusion .NET MAUI DataGrid (SfDataGrid).
77+
78+
##### Conclusion
79+
80+
I hope you enjoyed learning about how to get the cell value of specific row and column in .NET MAUI DataGrid (SfDataGrid).
81+
82+
You can refer to our [.NET MAUI DataGrid’s feature tour](https://www.syncfusion.com/maui-controls/maui-datagrid) page to learn about its other groundbreaking feature representations. You can also explore our [.NET MAUI DataGrid Documentation](https://help.syncfusion.com/maui/datagrid/getting-started) to understand how to present and manipulate data.
83+
For current customers, you can check out our .NET MAUI components on the [License and Downloads](https://www.syncfusion.com/sales/teamlicense) page. If you are new to Syncfusion, you can try our 30-day [free trial](https://www.syncfusion.com/downloads/maui) to explore our .NET MAUI DataGrid and other .NET MAUI components.
84+
85+
If you have any queries or require clarifications, please let us know in the comments below. You can also contact us through our [support forums](https://www.syncfusion.com/forums), [Direct-Trac](https://support.syncfusion.com/create) or [feedback portal](https://www.syncfusion.com/feedback/maui?control=sfdatagrid), or the feedback portal. We are always happy to assist you!

SfDataGridSample.sln

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.8.34330.188
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SfDataGridSample", "SfDataGridSample\SfDataGridSample.csproj", "{CA60F13B-F284-41B6-BC15-4F8A1A28503C}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{CA60F13B-F284-41B6-BC15-4F8A1A28503C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{CA60F13B-F284-41B6-BC15-4F8A1A28503C}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{CA60F13B-F284-41B6-BC15-4F8A1A28503C}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
17+
{CA60F13B-F284-41B6-BC15-4F8A1A28503C}.Release|Any CPU.ActiveCfg = Release|Any CPU
18+
{CA60F13B-F284-41B6-BC15-4F8A1A28503C}.Release|Any CPU.Build.0 = Release|Any CPU
19+
{CA60F13B-F284-41B6-BC15-4F8A1A28503C}.Release|Any CPU.Deploy.0 = Release|Any CPU
20+
EndGlobalSection
21+
GlobalSection(SolutionProperties) = preSolution
22+
HideSolutionNode = FALSE
23+
EndGlobalSection
24+
GlobalSection(ExtensibilityGlobals) = postSolution
25+
SolutionGuid = {582CF732-AF64-498B-80F8-5F82794E2678}
26+
EndGlobalSection
27+
EndGlobal

SfDataGridSample/App.xaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version = "1.0" encoding = "UTF-8" ?>
2+
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:SfDataGridSample"
5+
x:Class="SfDataGridSample.App">
6+
<Application.Resources>
7+
<ResourceDictionary>
8+
<ResourceDictionary.MergedDictionaries>
9+
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
10+
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
11+
</ResourceDictionary.MergedDictionaries>
12+
</ResourceDictionary>
13+
</Application.Resources>
14+
</Application>

SfDataGridSample/App.xaml.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace SfDataGridSample
2+
{
3+
public partial class App : Application
4+
{
5+
public App()
6+
{
7+
InitializeComponent();
8+
9+
MainPage = new AppShell();
10+
}
11+
}
12+
}

SfDataGridSample/AppShell.xaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<Shell
3+
x:Class="SfDataGridSample.AppShell"
4+
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:local="clr-namespace:SfDataGridSample"
7+
Shell.FlyoutBehavior="Disabled"
8+
Title="SfDataGridSample">
9+
10+
<ShellContent
11+
Title="Home"
12+
ContentTemplate="{DataTemplate local:MainPage}"
13+
Route="MainPage" />
14+
15+
</Shell>

SfDataGridSample/AppShell.xaml.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace SfDataGridSample
2+
{
3+
public partial class AppShell : Shell
4+
{
5+
public AppShell()
6+
{
7+
InitializeComponent();
8+
}
9+
}
10+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Syncfusion.Maui.DataGrid;
2+
using Syncfusion.Maui.DataGrid.Helper;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Diagnostics;
6+
using System.Linq;
7+
using System.Runtime.CompilerServices;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
namespace SfDataGridSample.Behaviors
12+
{
13+
public class ButtonBehavior : Behavior<Button>
14+
{
15+
protected override void OnAttachedTo(Button button)
16+
{
17+
base.OnAttachedTo(button);
18+
button.Clicked += OnButtonClicked!;
19+
}
20+
21+
protected override void OnDetachingFrom(Button button)
22+
{
23+
base.OnDetachingFrom(button);
24+
button.Clicked -= OnButtonClicked!;
25+
}
26+
27+
string? cellValue;
28+
private void OnButtonClicked(object sender, EventArgs e)
29+
{
30+
var button = sender as Button;
31+
var stackLayout = button!.Parent as StackLayout;
32+
var dataGrid = stackLayout!.Children[0] as SfDataGrid;
33+
foreach (var column in dataGrid!.Columns)
34+
{
35+
if (column.MappingName == "Name")
36+
{
37+
var rowData = dataGrid.GetRecordAtRowIndex(1);
38+
cellValue = dataGrid.GetCellValue(rowData, column.MappingName) as String;
39+
break;
40+
}
41+
}
42+
Application.Current!.MainPage!.DisplayAlert("ColumnName", cellValue, "Cancel");
43+
}
44+
}
45+
}

SfDataGridSample/MainPage.xaml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:syncfusion="clr-namespace:Syncfusion.Maui.DataGrid;assembly=Syncfusion.Maui.DataGrid"
5+
xmlns:local="clr-namespace:SfDataGridSample"
6+
xmlns:behaviors="clr-namespace:SfDataGridSample.Behaviors"
7+
x:Class="SfDataGridSample.MainPage">
8+
9+
<ContentPage.BindingContext>
10+
<local:EmployeeViewModel />
11+
</ContentPage.BindingContext>
12+
13+
<StackLayout>
14+
<syncfusion:SfDataGrid ItemsSource="{Binding Employees}"
15+
AutoGenerateColumnsMode="None"
16+
ColumnWidthMode="Auto"
17+
DefaultColumnWidth="155">
18+
19+
<syncfusion:SfDataGrid.Columns>
20+
<syncfusion:DataGridTextColumn MappingName="EmployeeID"
21+
HeaderText="Employee ID" />
22+
<syncfusion:DataGridTextColumn MappingName="Name"
23+
HeaderText="Name" />
24+
<syncfusion:DataGridTextColumn MappingName="IDNumber"
25+
HeaderText="ID Number" />
26+
</syncfusion:SfDataGrid.Columns>
27+
28+
</syncfusion:SfDataGrid>
29+
30+
<Button Text="Click"
31+
HorizontalOptions="StartAndExpand"
32+
WidthRequest="460"
33+
TextColor="Black">
34+
<Button.Behaviors>
35+
<behaviors:ButtonBehavior />
36+
</Button.Behaviors>
37+
</Button>
38+
</StackLayout>
39+
40+
</ContentPage>

SfDataGridSample/MainPage.xaml.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Syncfusion.Maui.DataGrid;
2+
using System.Globalization;
3+
4+
namespace SfDataGridSample
5+
{
6+
public partial class MainPage : ContentPage
7+
{
8+
public MainPage()
9+
{
10+
InitializeComponent();
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)