Skip to content

Commit 2d4dc97

Browse files
authored
How to merge the cells based on its content and value of a specific column in WinUI DataGrid
How to merge the cells based on its content and value of a specific column in WinUI DataGrid
1 parent f451512 commit 2d4dc97

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

README.md

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
This example describes how to merge the cells based on its content and value of a specific column in WinUI DataGrid.
66

7-
[WinUI DataGrid](https://www.syncfusion.com/winui-controls/datagrid) (SfDataGrid) does not provide the direct support to merge the cells based on the content and value of a specific column. You can merge the cells based on the content and value of a specific column by customizing the **GetRange** method and [QueryCoveredRange](https://help.syncfusion.com/cr/winui/Syncfusion.UI.Xaml.DataGrid.SfDataGrid.html#Syncfusion_UI_Xaml_DataGrid_SfDataGrid_QueryCoveredRange) event in DataGrid.
7+
[WinUI DataGrid](https://www.syncfusion.com/winui-controls/datagrid) (SfDataGrid) allows to merge the cells based on the content and value of a specific column in the same row by customizing the **GetRange** method and [QueryCoveredRange](https://help.syncfusion.com/cr/winui/Syncfusion.UI.Xaml.DataGrid.SfDataGrid.html#Syncfusion_UI_Xaml_DataGrid_SfDataGrid_QueryCoveredRange) event in DataGrid.
88

99
``` C#
1010

11-
this.sfDataGrid.ItemsSourceChanged += SfDataGrid_ItemsSourceChanged;
12-
this.sfDataGrid.QueryCoveredRange += SfDataGrid_QueryCoveredRange;
11+
this.sfDataGrid.ItemsSourceChanged += OnItemsSourceChanged;
12+
this.sfDataGrid.QueryCoveredRange += OnQueryCoveredRange;
1313

1414
/// <summary>
1515
/// Reflector for SfDataGrid’s data.
@@ -19,7 +19,7 @@ IPropertyAccessProvider reflector = null;
1919
/// <summary>
2020
/// ItemsSourceChanged event handler.
2121
/// </summary>
22-
private void SfDataGrid_ItemsSourceChanged(object sender, GridItemsSourceChangedEventArgs e)
22+
private void OnItemsSourceChanged(object sender, GridItemsSourceChangedEventArgs e)
2323
{
2424
if (sfDataGrid.View != null)
2525
reflector = sfDataGrid.View.GetPropertyAccessProvider();
@@ -30,12 +30,12 @@ private void SfDataGrid_ItemsSourceChanged(object sender, GridItemsSourceChanged
3030
/// <summary>
3131
/// QueryCoveredRange event handler
3232
/// </summary>
33-
private void SfDataGrid_QueryCoveredRange(object sender, GridQueryCoveredRangeEventArgs e)
33+
private void OnQueryCoveredRange(object sender, GridQueryCoveredRangeEventArgs e)
3434
{
3535
CoveredCellInfo range = null;
3636

3737
////here apply merge the cell based on Columns
38-
if (e.GridColumn.MappingName == "Product" || e.GridColumn.MappingName == "Country" || e.GridColumn.MappingName == "Discount")
38+
if (e.GridColumn.MappingName == "Product" || e.GridColumn.MappingName == "Country" )
3939
range = GetRange(e.GridColumn, e.RowColumnIndex.RowIndex, e.RowColumnIndex.ColumnIndex, e.Record);
4040

4141
if (range == null)
@@ -65,7 +65,7 @@ private CoveredCellInfo GetRange(GridColumn column, int rowIndex, int columnInde
6565
var range = new CoveredCellInfo(columnIndex, columnIndex, rowIndex, rowIndex);
6666
object data = reflector.GetFormattedValue(rowData, column.MappingName);
6767

68-
//here get the Product value for checikng other cell value
68+
//here get the Product value for checking other cell value
6969
string productData = (rowData as ProductSalesDetails).Product;
7070

7171
GridColumn leftColumn = null;
@@ -78,7 +78,7 @@ private CoveredCellInfo GetRange(GridColumn column, int rowIndex, int columnInde
7878

7979
// Merge Horizontally
8080
81-
// compare right column
81+
// compare right column
8282
for (int i = sfDataGrid.Columns.IndexOf(column); i < this.sfDataGrid.Columns.Count - 1; i++)
8383
{
8484
var compareData = reflector.GetFormattedValue(rowData, sfDataGrid.Columns[i + 1].MappingName);
@@ -128,7 +128,7 @@ private CoveredCellInfo GetRange(GridColumn column, int rowIndex, int columnInde
128128
int nextRowIndex = -1;
129129
object previousData = null;
130130

131-
// Get previous row data.
131+
// Get previous row data.
132132
var startIndex = sfDataGrid.ResolveStartIndexBasedOnPosition();
133133

134134
for (int i = rowIndex - 1; i >= startIndex; i--)
@@ -187,7 +187,7 @@ private CoveredCellInfo GetRange(GridColumn column, int rowIndex, int columnInde
187187
break;
188188
var compareData = reflector.GetFormattedValue((nextData as RecordEntry).Data, column.MappingName);
189189

190-
//get the next row data value of RigID
190+
//get the next row data value of Product
191191
string productNextData = ((nextData as RecordEntry).Data as ProductSalesDetails).Product;
192192

193193
if (compareData == null)
@@ -218,8 +218,6 @@ private CoveredCellInfo GetRange(GridColumn column, int rowIndex, int columnInde
218218

219219
```
220220

221-
![Shows the merged cells based on its content and value of a specific column in SfDataGrid](MergecellbasedonContent.gif)
222-
223221
The following screenshot shows the merged cells in DataGrid,
224222

225223
![Shows the merged cells in SfDataGrid](MergedCell.png)

0 commit comments

Comments
 (0)