This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
/
GridCheckMarksSelection.cs
299 lines (283 loc) · 10.9 KB
/
GridCheckMarksSelection.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;
using System.Windows.Forms;
using DevExpress.Utils.Drawing;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using System.Drawing;
using DevExpress.XtraGrid.Columns;
namespace GridLookUpEditCBMultipleSelection
{
public class GridCheckMarksSelection
{
protected GridView _view;
protected ArrayList selection;
GridColumn column;
RepositoryItemCheckEdit edit;
const int CheckboxIndent = 4;
public GridCheckMarksSelection(GridView view)
: this()
{ View = view; }
public GridView View
{
get { return _view; }
set {
if (_view != value)
{
Detach();
Attach(value);
}
}
}
public GridColumn CheckMarkColumn { get { return column; } }
public GridCheckMarksSelection()
{
selection = new ArrayList();
this.OnSelectionChanged();
}
public ArrayList Selection
{
get { return selection; }
set { selection = value; }
}
public int SelectedCount { get { return selection.Count; } }
public object GetSelectedRow(int index)
{ return selection[index]; }
public int GetSelectedIndex(object row)
{ return selection.IndexOf(row); }
public void ClearSelection()
{
selection.Clear();
Invalidate();
}
public void SelectAll()
{ SelectAll(null); }
public void SelectAll(object dataSource)
{
selection.Clear();
if (dataSource != null)
{
if (dataSource is ICollection)
selection.AddRange(((ICollection)dataSource));
this.OnSelectionChanged();
}
else
{
for (int i = 0; i < _view.DataRowCount; i++)
selection.Add(_view.GetRow(i));
}
Invalidate();
}
public delegate void SelectionChangedEventHandler(object sender, EventArgs e);
public event SelectionChangedEventHandler SelectionChanged;
public void OnSelectionChanged()
{
if (SelectionChanged != null)
{
EventArgs e = new EventArgs();
SelectionChanged(this, e);
}
}
public void SelectGroup(int rowHandle, bool select)
{
if (IsGroupRowSelected(rowHandle) && select) return;
for (int i = 0; i < _view.GetChildRowCount(rowHandle); i++)
{
int childRowHandle = _view.GetChildRowHandle(rowHandle, i);
if (_view.IsGroupRow(childRowHandle))
SelectGroup(childRowHandle, select);
else
SelectRow(childRowHandle, select, false);
}
Invalidate();
}
public void SelectRow(int rowHandle, bool select)
{ SelectRow(rowHandle, select, true); }
public void InvertRowSelection(int rowHandle)
{
if (View.IsDataRow(rowHandle))
SelectRow(rowHandle, !IsRowSelected(rowHandle));
if (View.IsGroupRow(rowHandle))
SelectGroup(rowHandle, !IsGroupRowSelected(rowHandle));
}
public bool IsGroupRowSelected(int rowHandle)
{
for (int i = 0; i < _view.GetChildRowCount(rowHandle); i++)
{
int row = _view.GetChildRowHandle(rowHandle, i);
if (_view.IsGroupRow(row))
{
if (!IsGroupRowSelected(row)) return false;
}
else
if (!IsRowSelected(row)) return false;
}
return true;
}
public bool IsRowSelected(int rowHandle)
{
if (_view.IsGroupRow(rowHandle))
return IsGroupRowSelected(rowHandle);
object row = _view.GetRow(rowHandle);
return GetSelectedIndex(row) != -1;
}
protected virtual void Attach(GridView view)
{
if (view == null) return;
selection.Clear();
this._view = view;
view.BeginUpdate();
try
{
edit = view.GridControl.RepositoryItems.Add("CheckEdit") as RepositoryItemCheckEdit;
column = view.Columns.Add();
column.OptionsColumn.AllowSort = DevExpress.Utils.DefaultBoolean.False;
column.Visible = true;
column.VisibleIndex = 0;
column.FieldName = "CheckMarkSelection";
column.Caption = "Mark";
column.OptionsColumn.ShowCaption = false;
column.OptionsColumn.AllowEdit = false;
column.OptionsColumn.AllowSize = false;
column.UnboundType = DevExpress.Data.UnboundColumnType.Boolean;
column.Width = GetCheckBoxWidth();
column.ColumnEdit = edit;
view.Click += new EventHandler(View_Click);
view.CustomDrawColumnHeader += new ColumnHeaderCustomDrawEventHandler(View_CustomDrawColumnHeader);
view.CustomDrawGroupRow += new RowObjectCustomDrawEventHandler(View_CustomDrawGroupRow);
view.CustomUnboundColumnData += new CustomColumnDataEventHandler(view_CustomUnboundColumnData);
view.KeyDown += new KeyEventHandler(view_KeyDown);
}
finally
{
view.EndUpdate();
}
}
protected virtual void Detach()
{
if (_view == null) return;
if (column != null)
column.Dispose();
if (edit != null)
{
_view.GridControl.RepositoryItems.Remove(edit);
edit.Dispose();
}
_view.Click -= new EventHandler(View_Click);
_view.CustomDrawColumnHeader -= new ColumnHeaderCustomDrawEventHandler(View_CustomDrawColumnHeader);
_view.CustomDrawGroupRow -= new RowObjectCustomDrawEventHandler(View_CustomDrawGroupRow);
_view.CustomUnboundColumnData -= new CustomColumnDataEventHandler(view_CustomUnboundColumnData);
_view.KeyDown -= new KeyEventHandler(view_KeyDown);
_view = null;
}
protected int GetCheckBoxWidth()
{
DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo info = edit.CreateViewInfo() as DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo;
int width = 0;
GraphicsInfo.Default.AddGraphics(null);
try
{
width = info.CalcBestFit(GraphicsInfo.Default.Graphics).Width;
}
finally
{
GraphicsInfo.Default.ReleaseGraphics();
}
return width + CheckboxIndent * 2;
}
protected void DrawCheckBox(Graphics g, Rectangle r, bool Checked)
{
DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo info;
DevExpress.XtraEditors.Drawing.CheckEditPainter painter;
DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs args;
info = edit.CreateViewInfo() as DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo;
painter = edit.CreatePainter() as DevExpress.XtraEditors.Drawing.CheckEditPainter;
info.EditValue = Checked;
info.Bounds = r;
info.CalcViewInfo(g);
args = new DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs(info, new DevExpress.Utils.Drawing.GraphicsCache(g), r);
painter.Draw(args);
args.Cache.Dispose();
}
void Invalidate()
{
_view.BeginUpdate();
_view.EndUpdate();
}
void SelectRow(int rowHandle, bool select, bool invalidate)
{
if (IsRowSelected(rowHandle) == select) return;
object row = _view.GetRow(rowHandle);
if (select)
selection.Add(row);
else
selection.Remove(row);
if (invalidate)
Invalidate();
//OnSelectionChanged();
}
void view_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e)
{
if (e.Column == CheckMarkColumn)
{
if (e.IsGetData)
e.Value = IsRowSelected(View.GetRowHandle(e.ListSourceRowIndex));
else
SelectRow(View.GetRowHandle(e.ListSourceRowIndex), (bool)e.Value);
}
}
void view_KeyDown(object sender, KeyEventArgs e)
{
if (View.FocusedColumn != column || e.KeyCode != Keys.Space) return;
InvertRowSelection(View.FocusedRowHandle);
}
void View_Click(object sender, EventArgs e)
{
GridHitInfo info;
Point pt = _view.GridControl.PointToClient(Control.MousePosition);
info = _view.CalcHitInfo(pt);
if (info.Column == column)
{
if (info.InColumn)
{
if (SelectedCount == _view.DataRowCount)
ClearSelection();
else
SelectAll();
}
if (info.InRowCell)
InvertRowSelection(info.RowHandle);
}
if (info.InRow && _view.IsGroupRow(info.RowHandle) && info.HitTest != GridHitTest.RowGroupButton)
InvertRowSelection(info.RowHandle);
}
void View_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
{
if (e.Column == column)
{
e.Info.InnerElements.Clear();
e.Painter.DrawObject(e.Info);
DrawCheckBox(e.Cache.Graphics, e.Bounds, SelectedCount == _view.DataRowCount);
e.Handled = true;
}
}
void View_CustomDrawGroupRow(object sender, RowObjectCustomDrawEventArgs e)
{
DevExpress.XtraGrid.Views.Grid.ViewInfo.GridGroupRowInfo info;
info = e.Info as DevExpress.XtraGrid.Views.Grid.ViewInfo.GridGroupRowInfo;
info.GroupText = " " + info.GroupText.TrimStart();
e.Cache.FillRectangle(e.Appearance.GetBackBrush(e.Cache), e.Bounds);
//e.Info.Paint.FillRectangle(e.Graphics, e.Appearance.GetBackBrush(e.Cache), e.Bounds);
e.Painter.DrawObject(e.Info);
Rectangle r = info.ButtonBounds;
r.Offset(r.Width + CheckboxIndent * 2 - 1, 0);
DrawCheckBox(e.Cache.Graphics, r, IsGroupRowSelected(e.RowHandle));
e.Handled = true;
}
}
}