dataGridView1 ColumnHeaderMouseCli ck obj evnt _stack Push evnt Column

 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
dataGridView1.ColumnHeaderMouseClick += (obj, evnt) =>
{
_stack.Push(evnt.ColumnIndex);
int columns = dataGridView1.Columns.Count;
int[] array = _stack.Distinct().ToArray();
var builder = new StringBuilder();
var listName = new List<string>();
var listSort = new List<string>();
var str = new StringBuilder();
for (int index = 0; index < array.Length; index++)
{
int i = array[index];
if (index >= columns)
{
break;
}
DataGridViewColumn gridViewColumn = dataGridView1.Columns[i];
string sort = null;
switch (gridViewColumn.HeaderCell.SortGlyphDirection)
{
case System.Windows.Forms.SortOrder.None:
case System.Windows.Forms.SortOrder.Ascending:
sort = "ASC";
break;
case System.Windows.Forms.SortOrder.Descending:
sort = "DESC";
break;
default:
throw new ArgumentOutOfRangeException();
}
listName.Add(gridViewColumn.Name);
listSort.Add(sort);
builder.AppendFormat("{0}, ", sort);
str.AppendFormat("{0}, ", gridViewColumn.Name);
}
string s1 = builder.ToString();
s1 = s1.Remove(s1.Length - 2);
string s = str.ToString();
s = s.Remove(s.Length - 2);
if (listName.Count != listSort.Count)
{
MessageBox.Show("Something went wrong!");
return;
}
for (int i = 0; i < listName.Count; i++)
{
dataGridView1.Sort(dataGridView1.Columns[listName[i]], listSort[i] == "ASC" ? ListSortDirection.Ascending : ListSortDirection.Descending);
}
//SortData(s);
Console.WriteLine(s);
};