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

Posted yitouniu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 GridView 鼠标热点追踪 高亮显示行的主要内容,如果未能解决你的问题,请参考以下文章

DevExpress 获取gridview 鼠标位置所在单元格

实现DevExpress GridControl 只有鼠标双击后才进行修改数据

DevExpress.XtraGrid.Views.Grid.GridView怎么禁用自带的Ctrl+c复制功能

用DevExpress中的GridView控件时,设置模板列为RichTextEdit类型,默认显示的字体更改不过来是啥问题?

devexpress 如何让gridview某一列可编辑其他列不允许编辑 编辑列嵌入下拉列表 求高手给代码

C# dev控件中gridview 我要确定某一个单元格的位置