-
Notifications
You must be signed in to change notification settings - Fork 4
/
RefreshHelperClass.cs
189 lines (169 loc) · 5.36 KB
/
RefreshHelperClass.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System;
using System.Collections;
using DevExpress.XtraGrid;
using DevExpress.Utils;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
namespace DevExpress.XtraGrid.Helpers {
public class RefreshHelper {
[Serializable]
public struct RowInfo{
public object Id;
public int level;
};
private GridView view;
private string keyFieldName;
private ArrayList saveExpList;
private ArrayList saveSelList;
private ArrayList saveMasterRowsList;
private int visibleRowIndex = -1;
public RefreshHelper(GridView view, string keyFieldName) {
this.view = view;
this.keyFieldName = keyFieldName;
}
public ArrayList SaveExpList{
get {
if(saveExpList == null)
saveExpList = new ArrayList();
return saveExpList;
}
}
public ArrayList SaveSelList{
get {
if(saveSelList == null)
saveSelList = new ArrayList();
return saveSelList;
}
}
public ArrayList SaveMasterRowsList{
get{
if(saveMasterRowsList == null)
saveMasterRowsList = new ArrayList();
return saveMasterRowsList;
}
}
protected int FindParentRowHandle(RowInfo rowInfo, int rowHandle) {
int result = view.GetParentRowHandle(rowHandle);
while(view.GetRowLevel(result) != rowInfo.level && view.IsValidRowHandle(result))
result = view.GetParentRowHandle(result);
return result;
}
protected void ExpandRowByRowInfo(RowInfo rowInfo){
int dataRowHandle = view.LocateByValue(0, view.Columns[keyFieldName], rowInfo.Id);
if(dataRowHandle != GridControl.InvalidRowHandle){
int parentRowHandle = FindParentRowHandle(rowInfo, dataRowHandle);
view.SetRowExpanded(parentRowHandle, true, false);
}
}
protected int GetRowHandleToSelect(RowInfo rowInfo) {
int dataRowHandle = view.LocateByValue(0, view.Columns[keyFieldName], rowInfo.Id);
if(dataRowHandle != GridControl.InvalidRowHandle)
if (view.GetRowLevel(dataRowHandle) != rowInfo.level)
return FindParentRowHandle(rowInfo, dataRowHandle);
return dataRowHandle;
}
protected void SelectRowByRowInfo(RowInfo rowInfo, bool isFocused){
if(isFocused)
view.FocusedRowHandle = GetRowHandleToSelect(rowInfo);
else
view.SelectRow(GetRowHandleToSelect(rowInfo));
}
public void SaveSelectionViewInfo(ArrayList list){
list.Clear();
GridColumn column = view.Columns[keyFieldName];
RowInfo rowInfo;
int[] selectionArray = view.GetSelectedRows();
if (selectionArray != null) // otherwise we have a single focused but not selected row
for(int i = 0; i < selectionArray.Length; i++){
int dataRowHandle = selectionArray[i];
rowInfo.level = view.GetRowLevel(dataRowHandle);
if(dataRowHandle < 0) // group row
dataRowHandle = view.GetDataRowHandleByGroupRowHandle(dataRowHandle);
rowInfo.Id = view.GetRowCellValue(dataRowHandle, column);
list.Add(rowInfo);
}
rowInfo.Id = view.GetRowCellValue(view.FocusedRowHandle, column);
rowInfo.level = view.GetRowLevel(view.FocusedRowHandle);
list.Add(rowInfo);
}
public void SaveExpansionViewInfo(ArrayList list){
if(view.GroupedColumns.Count == 0) return;
list.Clear();
GridColumn column = view.Columns[keyFieldName];
for(int i = -1; i > int.MinValue; i--) {
if(!view.IsValidRowHandle(i)) break;
if(view.GetRowExpanded(i)){
RowInfo rowInfo;
int dataRowHandle = view.GetDataRowHandleByGroupRowHandle(i);
rowInfo.Id = view.GetRowCellValue(dataRowHandle, column);
rowInfo.level = view.GetRowLevel(i);
list.Add(rowInfo);
}
}
}
public void SaveExpandedMasterRows(ArrayList list){
if(view.GridControl.Views.Count == 1) return;
list.Clear();
GridColumn column = view.Columns[keyFieldName];
for(int i = 0; i < view.DataRowCount; i++)
if(view.GetMasterRowExpanded(i))
list.Add(view.GetRowCellValue(i, column));
}
public void SaveVisibleIndex(){
visibleRowIndex = view.GetVisibleIndex(view.FocusedRowHandle) - view.TopRowIndex;
}
public void LoadVisibleIndex(){
view.MakeRowVisible(view.FocusedRowHandle, true);
view.TopRowIndex = view.GetVisibleIndex(view.FocusedRowHandle) - visibleRowIndex;
}
public void LoadSelectionViewInfo(ArrayList list) {
view.BeginSelection();
try {
view.ClearSelection();
for(int i = 0; i < list.Count; i++)
SelectRowByRowInfo((RowInfo)list[i], i == list.Count - 1);
}
finally {
view.EndSelection();
}
}
public void LoadExpansionViewInfo(ArrayList list) {
if(view.GroupedColumns.Count == 0) return;
view.BeginUpdate();
try {
view.CollapseAllGroups();
foreach(RowInfo info in list)
ExpandRowByRowInfo(info);
}
finally {
view.EndUpdate();
}
}
public void LoadExpandedMasterRows(ArrayList list) {
view.BeginUpdate();
try {
view.CollapseAllDetails();
GridColumn column = view.Columns[keyFieldName];
for(int i = 0; i < list.Count; i++){
int rowHandle = view.LocateByValue(0, column, list[i]);
view.SetMasterRowExpanded(rowHandle, true);
}
}
finally {
view.EndUpdate();
}
}
public void SaveViewInfo(){
SaveExpandedMasterRows(SaveMasterRowsList);
SaveExpansionViewInfo(SaveExpList);
SaveSelectionViewInfo(SaveSelList);
SaveVisibleIndex();
}
public void LoadViewInfo(){
LoadExpandedMasterRows(SaveMasterRowsList);
LoadExpansionViewInfo(SaveExpList);
LoadSelectionViewInfo(SaveSelList);
LoadVisibleIndex();
}
}
}