This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
103 lines (91 loc) · 4 KB
/
Form1.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
using DevExpress.Data;
using DevExpress.Utils;
using DevExpress.Utils.Drawing;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CustomDrawColumnHeader_SortButton {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
List<MyRecord> list = new List<MyRecord>();
list.Add(new MyRecord(0, "Steven Baum", "USA"));
list.Add(new MyRecord(1, "Robert McKinsey", "USA"));
list.Add(new MyRecord(2, "Robert McKinsey", "UK"));
list.Add(new MyRecord(3, "Daniella Lloyd", "UK"));
gridControl1.DataSource = list;
gridControl1.MainView.PopulateColumns();
(gridControl1.MainView as ColumnView).Columns["Country"].SortOrder = ColumnSortOrder.Ascending;
}
private void gridView1_CustomDrawColumnHeader(object sender, DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e) {
if (e.Column == null) return;
//A rectangle for the sort glyph
Rectangle sortBounds = Rectangle.Empty;
try {
//Store the rectangle for the sort glyph in sortBounds and
//then clear this region in the e.Info object
UpdateInnerElements(e, false, ref sortBounds);
//Draw the column header without the sort glyph
e.Painter.DrawObject(e.Info);
}
finally {
//Restore the rectangle for the sort glyph
UpdateInnerElements(e, true, ref sortBounds);
}
if (!sortBounds.IsEmpty) {
//Perform custom painting of the sort glyph
Rectangle newSortBounds = sortBounds;
newSortBounds.Offset(-10, -1);
DrawCustomSortedShape(e.Cache, newSortBounds, e.Column.SortOrder, sortGlyphCollection);
}
e.Handled = true;
}
void UpdateInnerElements(ColumnHeaderCustomDrawEventArgs e, bool restore, ref Rectangle sortBounds) {
//Locate an element corresponding to the sort glyph
foreach (DevExpress.Utils.Drawing.DrawElementInfo item in e.Info.InnerElements) {
if (item.ElementPainter is DevExpress.Utils.Drawing.SortedShapeObjectPainter) {
if (restore) {
//Restore the rectangle for the sort glyph
item.ElementInfo.Bounds = sortBounds;
}
else {
//Store the rectangle for the sort glyph in sortBounds and
//then clear this region in the e.Info object
sortBounds = item.ElementInfo.Bounds;
item.ElementInfo.Bounds = Rectangle.Empty;
}
}
}
}
private static void DrawCustomSortedShape(GraphicsCache cache, Rectangle r, ColumnSortOrder so, ImageCollection imCol) {
//Draw a custom image for the sort glyph
if (so == ColumnSortOrder.None) return;
int i = 0;
if (so == ColumnSortOrder.Descending) i = 1;
cache.Paint.DrawImageUnscaled(cache.Graphics, imCol.Images[i], new Point(r.X, r.Y));
}
private void timer1_Tick(object sender, EventArgs e) {
Text = "'Country' Column Sort Order = " + gridView1.Columns["Country"].SortOrder.ToString();
}
}
public class MyRecord {
public int ID { get; set; }
public string Country { get; set; }
public string Name { get; set; }
public MyRecord(int id, string name, string country) {
ID = id;
Name = name;
Country = country;
}
}
}