Skip to content

Commit 04584d3

Browse files
Merge pull request #1 from SyncfusionExamples/checkBoxRetrieve
How to retrieve the cell content using the rowIndex in a DataGridTemplateColumn?
2 parents bd00fc7 + 38d7435 commit 04584d3

40 files changed

+1654
-2
lines changed

README.md

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,95 @@
1-
# -How-to-retrieve-the-actual-checkBox-in-a-cell-using-rowIndex-in-a-DataGridTemplateColumn
2-
How-to-retrieve-the-actual-checkBox-in-a-cell-using-rowIndex-in-a-DataGridTemplateColumn
1+
# How to retrieve the cell content using the rowIndex in a DataGridTemplateColumn of the .NET MAUI DataGrid?
2+
In this article, we will show you how to retrieve the cell content using the rowIndex in a DataGridTemplateColumn of the [.Net Maui DataGrid](https://www.syncfusion.com/maui-controls/maui-datagrid).
3+
4+
## xaml
5+
```
6+
<ContentPage.BindingContext>
7+
<local:EmployeeViewModel x:Name="viewModel"/>
8+
</ContentPage.BindingContext>
9+
10+
<Grid>
11+
<Grid.RowDefinitions>
12+
<RowDefinition Height="*"/>
13+
<RowDefinition Height="50"/>
14+
</Grid.RowDefinitions>
15+
16+
<syncfusion:SfDataGrid x:Name="dataGrid"
17+
GridLinesVisibility="Both"
18+
HeaderGridLinesVisibility="Both"
19+
ColumnWidthMode="Auto"
20+
Grid.Row="0"
21+
Grid.Column="0"
22+
AutoGenerateColumnsMode="None"
23+
ItemsSource="{Binding Employees}">
24+
25+
<syncfusion:SfDataGrid.Columns>
26+
<syncfusion:DataGridTemplateColumn MappingName="EmployeeStatus"
27+
HeaderText="EmployeeStatus">
28+
<syncfusion:DataGridTemplateColumn.CellTemplate>
29+
<DataTemplate>
30+
<StackLayout>
31+
<CheckBox IsChecked="{Binding EmployeeStatus}"
32+
></CheckBox>
33+
</StackLayout>
34+
</DataTemplate>
35+
</syncfusion:DataGridTemplateColumn.CellTemplate>
36+
</syncfusion:DataGridTemplateColumn>
37+
<syncfusion:DataGridNumericColumn MappingName="EmployeeID"
38+
HeaderText="Employee ID"
39+
Format="#" />
40+
<syncfusion:DataGridTextColumn MappingName="Name"
41+
HeaderText="Employee Name" />
42+
<syncfusion:DataGridTextColumn MappingName="Title"
43+
HeaderText="Designation" />
44+
<syncfusion:DataGridDateColumn MappingName="HireDate"
45+
HeaderText="Hire Date" />
46+
47+
</syncfusion:SfDataGrid.Columns>
48+
</syncfusion:SfDataGrid>
49+
<Button Clicked="Button_Clicked"
50+
Grid.Row="1"
51+
Grid.Column="0"
52+
Text="Click"/>
53+
</Grid>
54+
```
55+
56+
## C#
57+
The below code illustrates how to retrieve the cell content using the rowIndex and mapping name in DataGrid.
58+
```
59+
private void Button_Clicked(object sender, EventArgs e)
60+
{
61+
GetCellContent(2, "EmployeeStatus");
62+
}
63+
64+
View? GetCellContent(int rowIndex, string mappingName)
65+
{
66+
var row = dataGrid.GetRowGenerator().Items?.FirstOrDefault(item => item.RowIndex == rowIndex);
67+
68+
if (row != null)
69+
{
70+
var columns = row.GetType().GetRuntimeProperties().FirstOrDefault(data => data.Name.Equals("VisibleColumns"))?.GetValue(row) as List<DataColumnBase>;
71+
if (columns != null)
72+
{
73+
var column = columns.FirstOrDefault(column => column.DataGridColumn != null && column.DataGridColumn.MappingName.Equals(mappingName));
74+
if (column != null)
75+
{
76+
return column.ColumnElement?.Content;
77+
}
78+
}
79+
}
80+
81+
return null;
82+
}
83+
```
84+
[View sample in GitHub](https://github.com/SyncfusionExamples/How-to-retrieve-the-actual-checkBox-in-a-cell-using-rowIndex-in-a-DataGridTemplateColumn)
85+
86+
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).
87+
88+
##### Conclusion
89+
90+
I hope you enjoyed learning about how to retrieve the cell content using the rowIndex in a DataGridTemplateColumn of the .NET MAUI DataGrid (SfDataGrid).
91+
92+
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.
93+
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.
94+
95+
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/App.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+
<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+
7+
<Application.Resources>
8+
<ResourceDictionary>
9+
<ResourceDictionary.MergedDictionaries>
10+
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
11+
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
12+
</ResourceDictionary.MergedDictionaries>
13+
</ResourceDictionary>
14+
</Application.Resources>
15+
</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+
}

SfDataGridSample/MainPage.xaml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
x:Class="SfDataGridSample.MainPage">
7+
8+
<ContentPage.BindingContext>
9+
<local:EmployeeViewModel x:Name="viewModel"/>
10+
</ContentPage.BindingContext>
11+
12+
<Grid>
13+
<Grid.RowDefinitions>
14+
<RowDefinition Height="*"/>
15+
<RowDefinition Height="50"/>
16+
</Grid.RowDefinitions>
17+
18+
<syncfusion:SfDataGrid x:Name="dataGrid"
19+
GridLinesVisibility="Both"
20+
HeaderGridLinesVisibility="Both"
21+
ColumnWidthMode="Auto"
22+
Grid.Row="0"
23+
Grid.Column="0"
24+
AutoGenerateColumnsMode="None"
25+
ItemsSource="{Binding Employees}">
26+
27+
<syncfusion:SfDataGrid.Columns>
28+
<syncfusion:DataGridTemplateColumn MappingName="EmployeeStatus"
29+
HeaderText="EmployeeStatus">
30+
<syncfusion:DataGridTemplateColumn.CellTemplate>
31+
<DataTemplate>
32+
<StackLayout>
33+
<CheckBox IsChecked="{Binding EmployeeStatus}"
34+
></CheckBox>
35+
</StackLayout>
36+
</DataTemplate>
37+
</syncfusion:DataGridTemplateColumn.CellTemplate>
38+
</syncfusion:DataGridTemplateColumn>
39+
<syncfusion:DataGridNumericColumn MappingName="EmployeeID"
40+
HeaderText="Employee ID"
41+
Format="#" />
42+
<syncfusion:DataGridTextColumn MappingName="Name"
43+
HeaderText="Employee Name" />
44+
<syncfusion:DataGridTextColumn MappingName="Title"
45+
HeaderText="Designation" />
46+
<syncfusion:DataGridDateColumn MappingName="HireDate"
47+
HeaderText="Hire Date" />
48+
49+
</syncfusion:SfDataGrid.Columns>
50+
</syncfusion:SfDataGrid>
51+
<Button Clicked="Button_Clicked"
52+
Grid.Row="1"
53+
Grid.Column="0"
54+
Text="Click"/>
55+
</Grid>
56+
</ContentPage>

SfDataGridSample/MainPage.xaml.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Microsoft.Maui.Controls;
2+
using Syncfusion.Maui.DataGrid;
3+
using Syncfusion.Maui.Themes;
4+
using System.Reflection;
5+
6+
namespace SfDataGridSample
7+
{
8+
public partial class MainPage : ContentPage
9+
{
10+
public MainPage()
11+
{
12+
InitializeComponent();
13+
}
14+
15+
private void Button_Clicked(object sender, EventArgs e)
16+
{
17+
GetCellContent(2, "EmployeeStatus");
18+
}
19+
20+
View? GetCellContent(int rowIndex, string mappingName)
21+
{
22+
var row = dataGrid.GetRowGenerator().Items?.FirstOrDefault(item => item.RowIndex == rowIndex);
23+
24+
if (row != null)
25+
{
26+
var columns = row.GetType().GetRuntimeProperties().FirstOrDefault(data => data.Name.Equals("VisibleColumns"))?.GetValue(row) as List<DataColumnBase>;
27+
if (columns != null)
28+
{
29+
var column = columns.FirstOrDefault(column => column.DataGridColumn != null && column.DataGridColumn.MappingName.Equals(mappingName));
30+
if (column != null)
31+
{
32+
return column.ColumnElement?.Content;
33+
}
34+
}
35+
}
36+
37+
return null;
38+
}
39+
}
40+
}

SfDataGridSample/MauiProgram.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.Extensions.Logging;
2+
using Syncfusion.Maui.Core;
3+
using Syncfusion.Maui.Core.Hosting;
4+
namespace SfDataGridSample
5+
{
6+
public static class MauiProgram
7+
{
8+
public static MauiApp CreateMauiApp()
9+
{
10+
var builder = MauiApp.CreateBuilder();
11+
builder
12+
.UseMauiApp<App>()
13+
.ConfigureSyncfusionCore()
14+
.ConfigureFonts(fonts =>
15+
{
16+
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
17+
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
18+
});
19+
20+
#if DEBUG
21+
builder.Logging.AddDebug();
22+
#endif
23+
24+
return builder.Build();
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)