在上篇文章C1FlexGrid DataMap列自定义排序:中,我们讲述了在C1FlexGrid设置DataMap列后,如何自定义排序。
本文就在此基础上,介绍如何按自己的规则绘制排序符号。
排序的时候,列依靠排序顺序来进行值排序。因此,你需要根据排序的顺序,给列添加新的排序符号。
这就需要使用C1FlexGrid的OwnerDraw事件,比较容易的绘制符号。当单元格重画的时候,这个事件就会被触发。
首先,在Form初始化的时候,设置ShowSortPosition为None,并设置DrawMode为OwnerDraw。这样子就放弃原本Sort符号,改由OwnerDraw来画。
_flex.ShowSortPosition = C1.Win.C1FlexGrid.ShowSortPositionEnum.None; _flex.DrawMode = C1.Win.C1FlexGrid.DrawModeEnum.OwnerDraw; _flex.OwnerDrawCell += new C1.Win.C1FlexGrid.OwnerDrawCellEventHandler(_flex_OwnerDrawCell);
然后,在OwnerDraw事件里,我们根据排序顺序绘制符号图像在header cell上。
void _flex_OwnerDrawCell(object sender, C1.Win.C1FlexGrid.OwnerDrawCellEventArgs e) { if (e.Row == 0) { e.Style.ImageAlign = C1.Win.C1FlexGrid.ImageAlignEnum.RightCenter; if (_sourceColumn!=null && e.Col == _sourceColumn.Index) { if (this._flex.Cols[e.Col].Sort == C1.Win.C1FlexGrid.SortFlags.Ascending) { e.Image = this._flex.Glyphs[C1.Win.C1FlexGrid.GlyphEnum.Ascending]; } else if (this._flex.Cols[e.Col].Sort == C1.Win.C1FlexGrid.SortFlags.Descending) { e.Image = this._flex.Glyphs[C1.Win.C1FlexGrid.GlyphEnum.Descending]; } } else { e.Image = null; } } }
本文的示例源代码如下:
如果你对ComponentOne感兴趣,请到我们的官网下载最新版本:/download/?pid=3
如果你有疑问,可以到GCDN论坛获得技术支持:http://gcdn.grapecity.com.cn/showforum-68.html