下列代码展示了如何以自定义方式绘制列标头,然后使用默认的绘制机制来绘制标准的筛选按钮和排序按钮。 为了自定义绘制列标头,接管了 GridView.CustomDrawColumnHeader 事件。

下面的插图展示了运行结果 (使用 Caramel 皮肤绘制 GridControl 以及排序按钮、筛选按钮):

C#CopyCode image复制代码
using DevExpress.XtraGrid.Columns;

// Specify custom colors that will be used to fill column headers
Random rand = new Random();
foreach(GridColumn column in gridView1.Columns) {
    column.AppearanceHeader.BackColor = Color.FromArgb(rand.Next(0, 255), 
       rand.Next(255), rand.Next(255));
    column.AppearanceHeader.BackColor2 = Color.FromArgb(rand.Next(0, 255), 
       rand.Next(255), rand.Next(255));
}
gridView1.CustomDrawColumnHeader += 
    new ColumnHeaderCustomDrawEventHandler(gridView1_CustomDrawColumnHeader);

void gridView1_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e) {
    if (e.Column == null) return;
    Rectangle rect = e.Bounds;
    ControlPaint.DrawBorder3D(e.Graphics, e.Bounds);
    Brush brush =
        e.Cache.GetGradientBrush(rect, e.Column.AppearanceHeader.BackColor, 
        e.Column.AppearanceHeader.BackColor2, e.Column.AppearanceHeader.GradientMode);
    rect.Inflate(-1, -1);
    // Fill column headers with the specified colors.
    e.Graphics.FillRectangle(brush, rect);
    e.Appearance.DrawString(e.Cache, e.Info.Caption, e.Info.CaptionRect);
    // Draw the filter and sort buttons.
    foreach (DevExpress.Utils.Drawing.DrawElementInfo info in e.Info.InnerElements) {
        DevExpress.Utils.Drawing.ObjectPainter.DrawObject(e.Cache, info.ElementPainter, 
            info.ElementInfo);
    }
    e.Handled = true;
}
Visual BasicCopyCode image复制代码
Imports DevExpress.XtraGrid.Columns

Dim rand As Random = New Random()
For Each column As GridColumn In gridView1.Columns
    column.AppearanceHeader.BackColor = Color.FromArgb(rand.Next(0, 255), _
    rand.Next(255), rand.Next(255))
    column.AppearanceHeader.BackColor2 = Color.FromArgb(rand.Next(0, 255), _
    rand.Next(255), rand.Next(255))
Next

Private Sub gridView1_CustomDrawColumnHeader(ByVal sender As System.Object, _
ByVal e As DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs) _
Handles gridView1.CustomDrawColumnHeader
    If (e.Column Is Nothing) Then Return
    Dim rect As Rectangle = e.Bounds
    ControlPaint.DrawBorder3D(e.Graphics, e.Bounds)
    Dim brush As Brush = _
        e.Cache.GetGradientBrush(rect, e.Column.AppearanceHeader.BackColor, _
        e.Column.AppearanceHeader.BackColor2, e.Column.AppearanceHeader.GradientMode)
    rect.Inflate(-1, -1)
    ' Fill column headers with the specified colors.
    e.Graphics.FillRectangle(brush, rect)
    e.Appearance.DrawString(e.Cache, e.Info.Caption, e.Info.CaptionRect)
    ' Draw the filter and sort buttons.
    For Each info As DevExpress.Utils.Drawing.DrawElementInfo In e.Info.InnerElements
        DevExpress.Utils.Drawing.ObjectPainter.DrawObject(e.Cache, info.ElementPainter, _
        info.ElementInfo)
    Next
    e.Handled = True
End Sub