Skip to content

Commit 30e48d5

Browse files
ES-975464 - Modfied the content
1 parent f83bcd3 commit 30e48d5

File tree

1 file changed

+196
-5
lines changed

1 file changed

+196
-5
lines changed

README.md

Lines changed: 196 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,200 @@
1-
# How to skip selection for summary rows?
1+
# How to Skip Selection for Summary Rows in WinForms DataGrid?
22

3-
## About the example
4-
5-
This example illustrates how to create a custom selection controller for the SfDataGrid to skip selection for the caption summary and group summary rows.
3+
This example illustrates how to create a custom selection controller for the [WinForms DataGrid](https://www.syncfusion.com/winforms-ui-controls/datagrid) (SfDataGrid) to skip selection for the caption summary and group summary rows.
64

75
By default, the group summary and caption summary rows will be selected on both mouse click and arrow keys navigation. You can skip the pointer selection and arrow key navigation for these summary rows by creating a custom SelectionController and overriding the [HandlePointerOperations](https://help.syncfusion.com/cr/windowsforms/Syncfusion.WinForms.DataGrid.Interactivity.RowSelectionController.html#Syncfusion_WinForms_DataGrid_Interactivity_RowSelectionController_HandlePointerOperations_Syncfusion_WinForms_DataGrid_Events_DataGridPointerEventArgs_Syncfusion_WinForms_GridCommon_ScrollAxis_RowColumnIndex_) and [ProcessArrowKeysForSingleMultipleSelection](https://help.syncfusion.com/cr/windowsforms/Syncfusion.WinForms.DataGrid.Interactivity.RowSelectionController.html#Syncfusion_WinForms_DataGrid_Interactivity_RowSelectionController_ProcessArrowKeysForSingleMultipleSelection_System_Windows_Forms_KeyEventArgs_) methods respectively.
86

9-
KB article: https://support.syncfusion.com/kb/article/8461/how-to-skip-selection-for-summary-rows-in-winforms-datagrid-sfdatagrid
7+
### C#
8+
9+
``` csharp
10+
public Form1()
11+
{
12+
InitializeComponent();
13+
this.sfDataGrid.SelectionController = new CustomRowSelectionController(this.sfDataGrid);
14+
}
15+
16+
public class CustomRowSelectionController : RowSelectionController
17+
{
18+
SfDataGrid DataGrid;
19+
public CustomRowSelectionController(SfDataGrid sfDataGrid)
20+
: base(sfDataGrid)
21+
{
22+
this.DataGrid = sfDataGrid;
23+
}
24+
25+
protected override void HandlePointerOperations(Syncfusion.WinForms.DataGrid.Events.DataGridPointerEventArgs args, RowColumnIndex rowColumnIndex)
26+
{
27+
if (this.IsCaptionSummaryRow(rowColumnIndex.RowIndex) || this.IsGroupSummaryRow(rowColumnIndex.RowIndex))
28+
return;
29+
base.HandlePointerOperations(args, rowColumnIndex);
30+
}
31+
32+
protected override void ProcessArrowKeysForSingleMultipleSelection(KeyEventArgs args)
33+
{
34+
if (args.KeyCode == Keys.Up)
35+
{
36+
this.DataGrid.MoveToCurrentCell(new RowColumnIndex(this.GetPreviousRecordRowIndex(this.DataGrid.CurrentCell.RowIndex), this.DataGrid.CurrentCell.ColumnIndex));
37+
}
38+
else if (args.KeyCode == Keys.Down)
39+
{
40+
this.DataGrid.MoveToCurrentCell(new RowColumnIndex(this.GetNextRecordRowIndex(this.DataGrid.CurrentCell.RowIndex), this.DataGrid.CurrentCell.ColumnIndex));
41+
}
42+
else
43+
base.ProcessArrowKeysForSingleMultipleSelection(args);
44+
}
45+
46+
bool IsCaptionSummaryRow(int rowIndex)
47+
{
48+
var startIndex = this.DataGrid.TableControl.ResolveStartIndexBasedOnPosition();
49+
var record = this.DataGrid.View.TopLevelGroup.DisplayElements[rowIndex - startIndex];
50+
return record != null && record is Group;
51+
}
52+
53+
bool IsGroupSummaryRow(int rowIndex)
54+
{
55+
var startIndex = this.DataGrid.TableControl.ResolveStartIndexBasedOnPosition();
56+
var record = this.DataGrid.View.TopLevelGroup.DisplayElements[rowIndex - startIndex];
57+
return record != null && record is SummaryRecordEntry;
58+
}
59+
60+
private int GetNextRecordRowIndex(int currentRowIndex)
61+
{
62+
int nextRecordRowIndex = currentRowIndex + 1;
63+
64+
if (nextRecordRowIndex > this.GetLastRowIndex(this.DataGrid))
65+
return this.DataGrid.CurrentCell.RowIndex;
66+
67+
if (!this.IsCaptionSummaryRow(nextRecordRowIndex) && !this.IsGroupSummaryRow(nextRecordRowIndex))
68+
return nextRecordRowIndex;
69+
else
70+
return GetNextRecordRowIndex(nextRecordRowIndex);
71+
}
72+
73+
private int GetLastRowIndex(SfDataGrid dataGrid)
74+
{
75+
if (dataGrid.View.Records.Count == 0)
76+
return -1;
77+
var footerCount = dataGrid.GetUnboundRowsCount(VerticalPosition.Bottom, true);
78+
int count = 0;
79+
int index = dataGrid.RowCount - (dataGrid.TableControl.GetTableSummaryCount(VerticalPosition.Bottom) + footerCount + 1);
80+
if (dataGrid.AddNewRowPosition == RowPosition.Bottom)
81+
index -= 1;
82+
if (dataGrid.FilterRowPosition == RowPosition.Bottom)
83+
index -= 1;
84+
for (int start = index; start >= 0; start--)
85+
{
86+
if (!dataGrid.TableControl.RowHeights.GetHidden(start, out count))
87+
return start;
88+
}
89+
return index;
90+
}
91+
92+
private int GetPreviousRecordRowIndex(int currentRowIndex)
93+
{
94+
int previousRecordRowIndex = currentRowIndex - 1;
95+
96+
if (previousRecordRowIndex <= 0)
97+
return this.DataGrid.CurrentCell.RowIndex;
98+
99+
if (!this.IsCaptionSummaryRow(previousRecordRowIndex) && !this.IsGroupSummaryRow(previousRecordRowIndex))
100+
return previousRecordRowIndex;
101+
else
102+
return GetPreviousRecordRowIndex(previousRecordRowIndex);
103+
}
104+
}
105+
```
106+
107+
### VB
108+
109+
``` vb
110+
Public Sub New()
111+
InitializeComponent()
112+
Me.sfDataGrid.SelectionController = New CustomRowSelectionController(Me.sfDataGrid)
113+
End Sub
114+
115+
Public Class CustomRowSelectionController Inherits RowSelectionController
116+
117+
Private DataGrid As SfDataGrid
118+
119+
Public Sub New(ByVal sfDataGrid As SfDataGrid)
120+
MyBase.New(sfDataGrid)
121+
Me.DataGrid = sfDataGrid
122+
End Sub
123+
124+
Protected Overrides Sub HandlePointerOperations(ByVal args As Syncfusion.WinForms.DataGrid.Events.DataGridPointerEventArgs, ByVal rowColumnIndex As RowColumnIndex)
125+
If Me.IsCaptionSummaryRow(rowColumnIndex.RowIndex) OrElse Me.IsGroupSummaryRow(rowColumnIndex.RowIndex) Then
126+
Return
127+
End If
128+
MyBase.HandlePointerOperations(args, rowColumnIndex)
129+
End Sub
130+
131+
Protected Overrides Sub ProcessArrowKeysForSingleMultipleSelection(ByVal args As KeyEventArgs)
132+
If args.KeyCode = Keys.Up Then
133+
Me.DataGrid.MoveToCurrentCell(New RowColumnIndex(Me.GetPreviousRecordRowIndex(Me.DataGrid.CurrentCell.RowIndex), Me.DataGrid.CurrentCell.ColumnIndex))
134+
ElseIf args.KeyCode = Keys.Down Then
135+
Me.DataGrid.MoveToCurrentCell(New RowColumnIndex(Me.GetNextRecordRowIndex(Me.DataGrid.CurrentCell.RowIndex), Me.DataGrid.CurrentCell.ColumnIndex))
136+
Else
137+
MyBase.ProcessArrowKeysForSingleMultipleSelection(args)
138+
End If
139+
End Sub
140+
141+
Private Function IsCaptionSummaryRow(ByVal rowIndex As Integer) As Boolean
142+
Dim startIndex = Me.DataGrid.TableControl.ResolveStartIndexBasedOnPosition()
143+
Dim record = Me.DataGrid.View.TopLevelGroup.DisplayElements(rowIndex - startIndex)
144+
Return record IsNot Nothing AndAlso TypeOf record Is Group
145+
End Function
146+
147+
Private Function IsGroupSummaryRow(ByVal rowIndex As Integer) As Boolean
148+
Dim startIndex = Me.DataGrid.TableControl.ResolveStartIndexBasedOnPosition()
149+
Dim record = Me.DataGrid.View.TopLevelGroup.DisplayElements(rowIndex - startIndex)
150+
Return record IsNot Nothing AndAlso TypeOf record Is SummaryRecordEntry
151+
End Function
152+
153+
Private Function GetNextRecordRowIndex(ByVal currentRowIndex As Integer) As Integer
154+
Dim nextRecordRowIndex As Integer = currentRowIndex + 1
155+
156+
If nextRecordRowIndex > Me.GetLastRowIndex(Me.DataGrid) Then
157+
Return Me.DataGrid.CurrentCell.RowIndex
158+
End If
159+
160+
If (Not Me.IsCaptionSummaryRow(nextRecordRowIndex)) AndAlso (Not Me.IsGroupSummaryRow(nextRecordRowIndex)) Then
161+
Return nextRecordRowIndex
162+
Else
163+
Return GetNextRecordRowIndex(nextRecordRowIndex)
164+
End If
165+
End Function
166+
167+
Private Function GetLastRowIndex(ByVal dataGrid As SfDataGrid) As Integer
168+
If dataGrid.View.Records.Count = 0 Then
169+
Return -1
170+
End If
171+
Dim footerCount = dataGrid.GetUnboundRowsCount(VerticalPosition.Bottom, True)
172+
Dim count As Integer = 0
173+
Dim index As Integer = dataGrid.RowCount - (dataGrid.TableControl.GetTableSummaryCount(VerticalPosition.Bottom) + footerCount + 1)
174+
If dataGrid.AddNewRowPosition = RowPosition.Bottom Then
175+
index -= 1
176+
End If
177+
If dataGrid.FilterRowPosition = RowPosition.Bottom Then
178+
index -= 1
179+
End If
180+
For start As Integer = index To 0 Step -1
181+
If Not dataGrid.TableControl.RowHeights.GetHidden(start, count) Then
182+
Return start
183+
End If
184+
Next start
185+
Return index
186+
End Function
187+
188+
Private Function GetPreviousRecordRowIndex(ByVal currentRowIndex As Integer) As Integer
189+
Dim previousRecordRowIndex As Integer = currentRowIndex - 1
190+
If previousRecordRowIndex <= 0 Then
191+
Return Me.DataGrid.CurrentCell.RowIndex
192+
End If
193+
If (Not Me.IsCaptionSummaryRow(previousRecordRowIndex)) AndAlso (Not Me.IsGroupSummaryRow(previousRecordRowIndex)) Then
194+
Return previousRecordRowIndex
195+
Else
196+
Return GetPreviousRecordRowIndex(previousRecordRowIndex)
197+
End If
198+
End Function
199+
End Class
200+
```

0 commit comments

Comments
 (0)