下列代码展示了如何通过 GridView.CustomDrawGroupRow 事件执行 分组行 的自定义绘制。 在本示例中,使用渐变刷填充分组行。 通过 GridView.GetGroupRowDisplayText 方法获取分组行的文本,并且使用阴影效果绘制。
为了使用“LemonChiffon”颜色绘制分组缩进块面板,也接管了 GridView.GroupLevelStyle 事件。
C# | 复制代码 |
---|---|
using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraGrid.Views.Base; using DevExpress.XtraGrid.Views.Grid.ViewInfo; using System.Drawing.Drawing2D; private void gridView1_CustomDrawGroupRow(object sender, RowObjectCustomDrawEventArgs e) { GridView view = sender as GridView; GridGroupRowInfo groupRowInfo = e.Info as GridGroupRowInfo; Rectangle groupRowBounds = groupRowInfo.DataBounds; Rectangle expandButtonBounds = groupRowInfo.ButtonBounds; Rectangle textBounds = e.Bounds; textBounds.X = expandButtonBounds.Right + 4; //A brush for the group row. Brush brush = e.Cache.GetGradientBrush(groupRowBounds, Color.LemonChiffon, Color.Tan, LinearGradientMode.Horizontal); //A brush for the region containing the expand button. Brush brushImage = Brushes.LemonChiffon; //Brushes to draw the text in the group row Brush brushText = Brushes.Black, brushTextShadow = Brushes.White; if(e.RowHandle == view.FocusedRowHandle) { brush = brushTextShadow = Brushes.DarkBlue; brushText = Brushes.White; } //Fill the rectangle of the group row without the expand button e.Graphics.FillRectangle(brush, groupRowBounds); //Draw a custom expand button ImageList iml = imlCustomDrawImages; e.Graphics.DrawImageUnscaled(iml.Images[(view.GetRowExpanded(e.RowHandle) ? 1 : 0)], expandButtonBounds); //Draw the group row text string s = view.GetGroupRowDisplayText(e.RowHandle); e.Appearance.DrawString(e.Cache, s, new Rectangle(textBounds.X +1, textBounds.Y + 1, textBounds.Width, textBounds.Height), brushTextShadow); e.Appearance.DrawString(e.Cache, s, textBounds, brushText); //Prevent default painting e.Handled = true; } private void gridView1_GroupLevelStyle(object sender, GroupLevelStyleEventArgs e) { //Specify the back color for the group indent panel e.LevelAppearance.BackColor = Color.LemonChiffon; } |
Visual Basic | 复制代码 |
---|---|
Imports DevExpress.XtraGrid.Views.Base Imports DevExpress.XtraGrid.Views.Grid Imports DevExpress.XtraGrid.Views.Grid.ViewInfo Imports System.Drawing.Drawing2D Private Sub GridView1_CustomDrawGroupRow(ByVal sender As Object, _ ByVal e As RowObjectCustomDrawEventArgs) Handles GridView1.CustomDrawGroupRow Dim view As GridView = CType(sender, GridView) Dim groupRowInfo As GridGroupRowInfo = e.Info Dim groupRowBounds As Rectangle = groupRowInfo.DataBounds Dim expandButtonBounds As Rectangle = groupRowInfo.ButtonBounds Dim textBounds As Rectangle = e.Bounds textBounds.X = expandButtonBounds.Right + 4 'A brush for the group row. Dim brush As Brush = e.Cache.GetGradientBrush(groupRowBounds, Color.LemonChiffon, _ Color.Tan, LinearGradientMode.Horizontal) 'A brush for the region containing the expand button. Dim brushImage As Brush = Brushes.LemonChiffon 'Brushes to draw the text in the group row Dim brushText As Brush = Brushes.Black, brushTextShadow As Brush = Brushes.White If e.RowHandle = view.FocusedRowHandle Then brush = Brushes.DarkBlue brushTextShadow = Brushes.DarkBlue brushText = Brushes.White End If 'Fill the rectangle of the group row without the expand button e.Graphics.FillRectangle(brush, groupRowBounds) 'Draw a custom expand button Dim iml As ImageList = imlCustomDrawImages e.Graphics.DrawImageUnscaled(iml.Images(IIf(view.GetRowExpanded(e.RowHandle), 1, 0)), _ expandButtonBounds) 'Draw the group row text Dim s As String = view.GetGroupRowDisplayText(e.RowHandle) e.Appearance.DrawString(e.Cache, s, New Rectangle(textBounds.X + 1, textBounds.Y + 1, _ textBounds.Width, textBounds.Height), brushTextShadow) e.Appearance.DrawString(e.Cache, s, textBounds, brushText) 'Prevent default painting e.Handled = True End Sub Private Sub advBandedGridView1_GroupLevelStyle(ByVal sender As Object, _ ByVal e As GroupLevelStyleEventArgs) Handles advBandedGridView1.GroupLevelStyle ' Specify the back color for the group indent panel e.LevelAppearance.BackColor = Color.LemonChiffon End Sub |