DevExpress GridView 鼠标热点追踪 高亮显示行

版本:DevExpress 18.1 for Winform;.NET Framework 4.0。

GridControlUtils工具类的功能:

1,初始化GridView,奇偶行用不同颜色显示。默认,不支持编辑行,仅支持全行单选。

2,自动生成行号。

3,追踪鼠标并高亮显示鼠标所在行。

public static class GridControlUtils
{
    public static readonly Color BackColorOdd = Color.FromArgb(245, 248, 253);
    public static readonly Color BackColorEven = Color.FromArgb(229, 255, 229);
    public static readonly Color BackColorSelected = Color.FromArgb(59, 185, 255);
    public static readonly Color BackColorHover = Color.FromArgb(236, 197, 192);
    public static readonly Color ForeColorDefault = Color.Black;
    public static readonly Color ForeColorSelected = Color.White;

    public static void InitializeGridView(GridView gv)
    {
        gv.OptionsBehavior.AllowAddRows = DefaultBoolean.False;
        gv.OptionsBehavior.AllowDeleteRows = DefaultBoolean.False;
        gv.OptionsBehavior.AutoPopulateColumns = false;
        gv.OptionsBehavior.AutoSelectAllInEditor = false;
        gv.OptionsBehavior.Editable = false;
        gv.OptionsBehavior.ReadOnly = true;

        gv.OptionsCustomization.AllowColumnResizing = false;
        gv.OptionsCustomization.AllowFilter = false;
        gv.OptionsCustomization.AllowGroup = false;
        gv.OptionsCustomization.AllowRowSizing = false;
        gv.OptionsCustomization.AllowSort = false;

        gv.ColumnPanelRowHeight = 28;
        gv.RowHeight = 24;
        gv.IndicatorWidth = 50;
        gv.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
        gv.OptionsView.ColumnHeaderAutoHeight = DefaultBoolean.False;
        gv.OptionsView.EnableAppearanceEvenRow = true;
        gv.OptionsView.EnableAppearanceOddRow = true;
        gv.OptionsView.ShowAutoFilterRow = false;
        gv.OptionsView.RowAutoHeight = false;
        gv.OptionsView.ShowGroupPanel = false;

        gv.Appearance.HeaderPanel.Options.UseTextOptions = true;
        gv.Appearance.HeaderPanel.TextOptions.HAlignment = HorzAlignment.Center;
        gv.Appearance.HeaderPanel.TextOptions.VAlignment = VertAlignment.Center;
        gv.Appearance.HeaderPanel.TextOptions.WordWrap = WordWrap.NoWrap;
        gv.Appearance.Row.TextOptions.HAlignment = HorzAlignment.Near;
        gv.Appearance.Row.TextOptions.VAlignment = VertAlignment.Center;
        gv.Appearance.Row.TextOptions.WordWrap = WordWrap.NoWrap;
        gv.Appearance.OddRow.BackColor = GridControlUtils.BackColorOdd;
        gv.Appearance.OddRow.ForeColor = GridControlUtils.ForeColorDefault;
        gv.Appearance.EvenRow.BackColor = GridControlUtils.BackColorEven;
        gv.Appearance.EvenRow.ForeColor = GridControlUtils.ForeColorDefault;
        gv.Appearance.FocusedRow.BackColor = GridControlUtils.BackColorSelected;
        gv.Appearance.FocusedRow.ForeColor = GridControlUtils.ForeColorSelected;

        gv.OptionsSelection.EnableAppearanceFocusedCell = false;
        gv.OptionsSelection.EnableAppearanceFocusedRow = true;
        gv.OptionsSelection.EnableAppearanceHideSelection = false;
        gv.OptionsSelection.MultiSelect = false;

        gv.CustomDrawRowIndicator += (sender, e) =>
        {
            if (e.RowHandle >= 0)
            {
                var view = sender as GridView;
                e.Info.DisplayText = e.RowHandle + 1 + "";
            }
        };

        gv.RowCountChanged += (sender, e) =>
        {
            gv.InvalidateRows();
        };

        var hottrack = GridControl.InvalidRowHandle;
        gv.MouseMove += (sender, e) =>
        {
            var grid = (GridView)sender;
            var hitInfo = grid.CalcHitInfo(e.Location);
            if (hitInfo.RowHandle != hottrack)
            {
                if (hottrack >= 0)
                {
                    grid.RefreshRow(hottrack);
                }

                hottrack = hitInfo.RowHandle;

                if (hitInfo.InRow)
                {
                    grid.RefreshRow(hottrack);
                }
            }
        };

        gv.RowCellStyle += (sender, e) =>
        {
            var grid = (GridView)sender;
            if (e.RowHandle == hottrack && grid.FocusedRowHandle != hottrack)
            {
                e.Appearance.BackColor = GridControlUtils.BackColorHover;
                e.Appearance.Options.UseBackColor = true;
            }
        };

        //gv.RowStyle += (sender, e) =>
        //{
        //    var grid = (GridView)sender;
        //    if (e.RowHandle == hottrack && grid.FocusedRowHandle != hottrack)
        //    {
        //        e.Appearance.BackColor = GridControlUtils.BackColorHover;
        //        e.HighPriority = true;
        //    }
        //};
    }
}

说明:不建议使用GridView.RowStyle。因为此事件的参数e.Appearance会影响居中显示或居右显示的列。

上一篇:DevExpress Winforms界面开发,带你了解Visual Studio Palette Editor!


下一篇:Devexpress的Datalayout控件