-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
171 lines (150 loc) · 5.98 KB
/
Copy pathForm1.cs
File metadata and controls
171 lines (150 loc) · 5.98 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
using DataGridViewAutoFilter;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
namespace Dynamic_If
{
public partial class Form1 : Form
{
BindingList<Data> data;
public Form1()
{
InitializeComponent();
data = new BindingList<Data>
{
new Data(1, 100, 23.5f, (UInt16)34, "test1"),
new Data(2, 200, -23.5f, (UInt16)45, "test2"),
new Data(3, 100, 45.1f, (UInt16)56, "test3"),
new Data(4, 200, 67.3f, (UInt16)67, "test4"),
new Data(5, 100, 56.7f, (UInt16)78, "test5"),
new Data(6, 200, 45.1f, (UInt16)89, "test6"),
new Data(7, 100, 89.9f, (UInt16)90, "test7"),
new Data(8, 200, 78.8f, (UInt16)91, "test8"),
new Data(9, 250, 23.5f, (UInt16)34, "test9"),
new Data(10, 250, 89.9f, (UInt16)90, "test10")
};
dataGridView.DataSource = data;
dataGridView.ColumnHeaderMouseClick += DataGridView_ColumnHeaderMouseClick;
dataGridView.AutoGenerateColumns = false;
dataGridView.Update();
}
private List<(int columnIndex, SortOrder sortOrder)> sortOrders = new List<(int columnIndex, SortOrder sortOrder)>();
private void DataGridView_ColumnHeaderMouseClick(object? sender, DataGridViewCellMouseEventArgs e)
{
var column = dataGridView.Columns[e.ColumnIndex];
var existingSortOrder = sortOrders.FirstOrDefault(so => so.columnIndex == e.ColumnIndex);
SortOrder newOrder = SortOrder.Ascending;
if (existingSortOrder.sortOrder == SortOrder.Ascending)
{
newOrder = SortOrder.Descending;
}
else if (existingSortOrder.sortOrder == SortOrder.Descending)
{
newOrder = SortOrder.None;
}
if (newOrder == SortOrder.None)
{
sortOrders.Remove(existingSortOrder);
}
else
{
if (existingSortOrder.sortOrder == SortOrder.None)
{
sortOrders.Add((e.ColumnIndex, newOrder));
}
else
{
var index = sortOrders.FindIndex(so => so.columnIndex == e.ColumnIndex);
sortOrders[index] = (e.ColumnIndex, newOrder);
}
}
SortRows();
UpdateColumnHeaders();
}
private void SortRows()
{
IOrderedEnumerable<Data>? sortedData = null;
foreach (var sortOrder in sortOrders)
{
var columnName = dataGridView.Columns[sortOrder.columnIndex].Name;
var currentSorting = sortOrder.sortOrder;
switch (columnName)
{
case "a":
sortedData = Sort(sortedData, d => d.a, currentSorting);
break;
case "b":
sortedData = Sort(sortedData, d => d.b, currentSorting);
break;
case "c":
sortedData = Sort(sortedData, d => d.c, currentSorting);
break;
case "d":
sortedData = Sort(sortedData, d => d.d, currentSorting);
break;
case "e":
sortedData = Sort(sortedData, d => d.e, currentSorting);
break;
}
}
dataGridView.DataSource = new BindingList<Data>(sortedData?.ToList() ?? data.ToList());
}
private IOrderedEnumerable<Data> Sort(IOrderedEnumerable<Data>? sortedData, Func<Data, object> func, SortOrder currentSorting)
{
return sortedData == null
? currentSorting == SortOrder.Ascending
? data.OrderBy(func)
: data.OrderByDescending(func)
: currentSorting == SortOrder.Ascending
? sortedData.ThenBy(func)
: sortedData.ThenByDescending(func);
}
private void UpdateColumnHeaders()
{
foreach (DataGridViewColumn column in dataGridView.Columns)
{
column.HeaderCell.SortGlyphDirection = SortOrder.None;
}
foreach (var sortOrder in sortOrders)
{
dataGridView.Columns[sortOrder.columnIndex].HeaderCell.SortGlyphDirection = sortOrder.sortOrder;
}
}
private void sendButton_Click(object sender, EventArgs e) { ActionFunc(); }
private void expressionText_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
ActionFunc();
e.Handled = true;
e.SuppressKeyPress = true;
}
}
private void ActionFunc()
{
if (string.IsNullOrEmpty(expressionText.Text))
{
dataGridView.DataSource = data;
return;
}
PropertySelector<Data> ps = new PropertySelector<Data>(SelectorFunc);
dataGridView.DataSource = (new DynamicExpressions<Data>(ps)).Evaluate(data, expressionText.Text);
}
private object SelectorFunc(Data data, string property)
{
switch (property)
{
case "a": return data.a;
case "b": return data.b;
case "c": return data.c;
case "d": return data.d;
case "e": return data.e;
case "true": return true;
case "false": return false;
default: throw new ArgumentException("Invalid property: " + property);
}
}
}
}