Skip to content

Commit 892758f

Browse files
authored
Merge pull request #3 from SyncfusionExamples/ES-975464
ES-975464 - Resolve the Issues in the Public Repositories
2 parents 22d09f3 + 94b48fe commit 892758f

File tree

1 file changed

+199
-3
lines changed

1 file changed

+199
-3
lines changed

README.md

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

3-
## About the example
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.
44

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

0 commit comments

Comments
 (0)