-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDataGridViewUtilities.cs
More file actions
203 lines (122 loc) · 6.75 KB
/
DataGridViewUtilities.cs
File metadata and controls
203 lines (122 loc) · 6.75 KB
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace Gsemac.Forms {
public static class DataGridViewUtilities {
// Public members
public static IEnumerable<DataGridViewRow> GetVisibleRows(DataGridView dataGridView) {
List<DataGridViewRow> visibleRows = new List<DataGridViewRow>();
int visibleRowsCount = dataGridView.DisplayedRowCount(true);
if (visibleRowsCount > 0) {
int firstDisplayedRowIndex = dataGridView.FirstDisplayedCell.RowIndex;
for (int i = firstDisplayedRowIndex; i < dataGridView.Rows.Count && visibleRows.Count < visibleRowsCount; ++i) {
if (dataGridView.Rows[i].Visible)
visibleRows.Add(dataGridView.Rows[i]);
}
}
return visibleRows;
}
public static Rectangle GetVisibleRowBounds(DataGridView dataGridView, int rowIndex, bool includeHeaders = true) {
Debug.Assert(rowIndex >= 0);
Debug.Assert(rowIndex < dataGridView.Rows.Count);
if (!dataGridView.Rows[rowIndex].Visible || dataGridView.Columns.Count <= 0)
return new Rectangle(0, 0, 0, 0);
Rectangle rowBounds = dataGridView.GetRowDisplayRectangle(rowIndex, true);
// GetRowDisplayRectangle returns the entire row rectangle, whether or not there are actually columns all the way across.
// Subtract the area beyond the last column.
Rectangle lastColumnBounds = dataGridView.GetColumnDisplayRectangle(dataGridView.Columns.GetLastColumn(DataGridViewElementStates.Visible, DataGridViewElementStates.None).Index, true);
int rowRight = rowBounds.X + rowBounds.Width;
int lastColumnRight = lastColumnBounds.X + lastColumnBounds.Width;
if (lastColumnRight < rowRight)
rowBounds = new Rectangle(rowBounds.X, rowBounds.Y, rowBounds.Width - (rowRight - lastColumnRight), rowBounds.Height);
// If headers are not included, subtract the row reader.
if (!includeHeaders && dataGridView.RowHeadersVisible)
rowBounds = new Rectangle(rowBounds.X + dataGridView.RowHeadersWidth, rowBounds.Y, rowBounds.Width - dataGridView.RowHeadersWidth, rowBounds.Height);
return rowBounds;
}
public static Rectangle GetVisibleRowsBounds(DataGridView dataGridView, bool includeHeaders = true) {
IEnumerable<DataGridViewRow> visibleRows = GetVisibleRows(dataGridView);
// If there are no visible rows, an empty rectangle will be returned.
Rectangle bounds = new Rectangle(0, 0, 0, 0);
if (visibleRows.Count() > 0)
bounds = new Rectangle(int.MaxValue, int.MaxValue, 0, 0);
// If headers are included, include the column headers.
if (includeHeaders)
bounds = new Rectangle(bounds.X, 0, bounds.Width, dataGridView.ColumnHeadersHeight);
foreach (DataGridViewRow row in visibleRows) {
Rectangle rowBounds = GetVisibleRowBounds(dataGridView, row.Index, includeHeaders);
bounds = new Rectangle(
Math.Min(bounds.X, rowBounds.X),
Math.Min(bounds.Y, rowBounds.Y),
Math.Max(bounds.Width, rowBounds.Width),
bounds.Height + rowBounds.Height
);
}
return bounds;
}
public static bool IsRowHeaderIndex(int columnIndex) {
return columnIndex < 0;
}
public static bool IsColumnHeaderIndex(int rowIndex) {
return rowIndex < 0;
}
public static DataGridViewCellStyle GetDefaultCellStyle(DataGridView dataGridView, int rowIndex, int columnIndex) {
if (IsColumnHeaderIndex(rowIndex))
return dataGridView.ColumnHeadersDefaultCellStyle;
if (IsRowHeaderIndex(columnIndex))
return dataGridView.RowHeadersDefaultCellStyle;
return dataGridView.DefaultCellStyle;
}
public static void SetSmoothResizingEnabled(DataGridView dataGridView, bool enabled) {
if (enabled) {
dataGridView.MouseDown += DataGridViewSmoothResizingMouseDownHandler;
dataGridView.MouseMove += DataGridViewSmoothResizingMouseMoveHandler;
dataGridView.MouseUp += DataGridViewSmoothResizingMouseUpHandler;
}
else {
dataGridView.MouseDown -= DataGridViewSmoothResizingMouseDownHandler;
dataGridView.MouseMove -= DataGridViewSmoothResizingMouseMoveHandler;
dataGridView.MouseUp -= DataGridViewSmoothResizingMouseUpHandler;
}
}
// Private members
// These variables will be used globally for all DataGridViews that have real-time resizing enabled
private static bool columnResizing = false;
private static int columnResized = 0;
private static int columnOffset = 0;
private static int columnWidth = 0;
private static void DataGridViewSmoothResizingMouseDownHandler(object sender, MouseEventArgs e) {
try {
if (sender is DataGridView dataGridView && Cursor.Current == Cursors.SizeWE) {
DataGridView.HitTestInfo hitInfo = dataGridView.HitTest(e.X, e.Y);
columnResized = hitInfo.ColumnIndex;
// If within 8 pixels of the left-side column, assume that it is the left-side column that is being resized.
if (e.X < hitInfo.ColumnX + 8) {
hitInfo = dataGridView.HitTest(e.X - 8, e.Y);
columnResized = hitInfo.ColumnIndex;
}
columnOffset = e.X;
columnWidth = dataGridView.Columns[columnResized].Width;
columnResizing = true;
}
}
catch (Exception) { }
}
private static void DataGridViewSmoothResizingMouseMoveHandler(object sender, MouseEventArgs e) {
if (sender is DataGridView dataGridView && columnResizing) {
if (e.X > columnOffset)
dataGridView.Columns[columnResized].Width = columnWidth + (e.X - columnOffset);
else
dataGridView.Columns[columnResized].Width = columnWidth - (columnOffset - e.X);
}
}
private static void DataGridViewSmoothResizingMouseUpHandler(object sender, MouseEventArgs e) {
if (Cursor.Current == Cursors.SizeWE) {
columnResizing = false;
}
}
}
}