|
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! |
0 commit comments