This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
/
RowAndColumnActions.cs
126 lines (106 loc) · 4.39 KB
/
RowAndColumnActions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using DevExpress.Spreadsheet;
using System.Drawing;
using DevExpress.Xpf.Spreadsheet;
namespace SpreadsheetControl_WPF_Examples
{
public static class RowAndColumnActions {
#region Actions
public static Action<SpreadsheetControl> FreezeRowAction = FreezeRow;
public static Action<SpreadsheetControl> FreezeColumnAction = FreezeColumn;
public static Action<SpreadsheetControl> FreezePanesAction = FreezePanes;
public static Action<SpreadsheetControl> UnfreezePanesAction = UnfreezePanes;
#endregion
#region #FreezeRow
static void FreezeRow(SpreadsheetControl control) {
control.BeginUpdate();
try {
//Access the active worksheet.
Worksheet worksheet = control.Document.Worksheets.ActiveWorksheet;
// Access the cell range that is currently visible.
Range visibleRange = control.VisibleRange;
//Range visibleRange = control.Document.Range.FromLTRB(10, 15, 15, 20);
// Freeze the top visible row.
worksheet.FreezeRows(0, visibleRange);
}
finally {
control.EndUpdate();
}
}
#endregion #FreezeRow
#region #FreezeColumn
static void FreezeColumn(SpreadsheetControl control)
{
control.BeginUpdate();
try
{
//Access the active worksheet.
Worksheet worksheet = control.Document.Worksheets.ActiveWorksheet;
// Access the cell range that is currently visible.
Range visibleRange = control.VisibleRange;
// Freeze the top visible row.
worksheet.FreezeColumns(0, visibleRange);
}
finally
{
control.EndUpdate();
}
}
#endregion #FreezeColumn
#region #FreezePanes
static void FreezePanes(SpreadsheetControl control)
{
//Access the active worksheet.
Worksheet worksheet = control.Document.Worksheets.ActiveWorksheet;
// Access the cell range that is currently visible.
Range visibleRange = control.VisibleRange;
// Access the active cell.
Cell activeCell = control.ActiveCell;
int rowOffset = activeCell.RowIndex - visibleRange.TopRowIndex - 1;
int columnOffset = activeCell.ColumnIndex - visibleRange.LeftColumnIndex - 1;
// If the active cell is outside the visible range of cells, no rows and columns are frozen.
if (!visibleRange.IsIntersecting(activeCell))
{
return;
}
if (activeCell.ColumnIndex == visibleRange.LeftColumnIndex)
{
// If the active cell matches the top left visible cell, no rows and columns are frozen.
if (activeCell.RowIndex == visibleRange.TopRowIndex) { return; }
else
// Freeze visible rows above the active cell if it is located in the leftmost visible column.
worksheet.FreezeRows(rowOffset, visibleRange);
}
else if (activeCell.RowIndex == visibleRange.TopRowIndex)
{
// Freeze visible columns to the left of the active cell if it is located in the topmost visible row.
worksheet.FreezeColumns(columnOffset, visibleRange);
}
else
{
// Freeze both rows and columns above and to the left of the active cell.
worksheet.FreezePanes(rowOffset, columnOffset, visibleRange);
}
}
#endregion #FreezePanes
#region #UnfreezePanes
static void UnfreezePanes(SpreadsheetControl control)
{
control.BeginUpdate();
try
{
//Access the active worksheet.
Worksheet worksheet = control.Document.Worksheets.ActiveWorksheet;
// Access the cell range that is currently visible.
Range visibleRange = control.VisibleRange;
// Freeze the top visible row.
worksheet.FreezeRows(0, visibleRange);
}
finally
{
control.EndUpdate();
}
}
#endregion #UnfreezePanes
}
}