你如何突出显示DataGridView的行和/或列标签上(在C#)的任意单元格的鼠标悬停? [英] How do you highlight the row and/or column labels of a datagridview on mouseover of any cell (in c#)?

查看:124
本文介绍了你如何突出显示DataGridView的行和/或列标签上(在C#)的任意单元格的鼠标悬停?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Windows窗体上的DataGridView控件,当你将鼠标移动到一个行标签(或列标签),它的(标记细胞),这取决于您的Windows配色方案没有背景变为蓝色(或其它颜色的色光。怀疑)

With a DataGridView control on a Windows form, when you move the mouse over a row label (or column label) it's (the label cell) background changes to a shade of blue (or other colour depending on your Windows colour scheme no doubt).

我想产生过任何单元网格中移动鼠标时效果 - 即突出显示鼠标当前悬停在该行的行标签

I would like to produce that effect when moving the mouse over any cell in the grid - i.e. highlight the row label for the row the mouse is currently hovering over.

改变当前行的样式的逻辑是很简单的使用鼠标悬停事件。我可以改变(如说背景色)行的其他属性,但我真的很喜欢的东西比这更微妙的,我想强调的行标签会非常有效。

The logic for changing the style of the current row is simple enough using mouseover event. I can change other attributes of the row (like say the backcolor) but I would really like something more subtle than that and I think highlighting of the row label would be very effective.

能不能做到 - 如果又如何? (C#最好)

Can it be done - if so how? (C# preferably)

推荐答案

您可以覆盖的 OnCellPainting 事件做你想做的。根据您的的DataGridView 的大小, 。可能会看到闪烁,但这应该做你想做的。

You could override the OnCellPainting event to do what you want. Depending on the size of your DataGridView, you might see flickering, but this should do what you want.


    class MyDataGridView : DataGridView
    {
        private int mMousedOverColumnIndex = int.MinValue;
        private int mMousedOverRowIndex = int.MinValue;

        protected override void OnCellMouseEnter(DataGridViewCellEventArgs e)
        {
            mMousedOverColumnIndex = e.ColumnIndex;
            mMousedOverRowIndex = e.RowIndex;
            base.OnCellMouseEnter(e);
            base.Refresh();
        }

        protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
        {
            if (((e.ColumnIndex == mMousedOverColumnIndex) && (e.RowIndex == -1)) ||
                ((e.ColumnIndex == -1) && (e.RowIndex == mMousedOverRowIndex)))
            {
                PaintColumnHeader(e, System.Drawing.Color.Red);
            }
            base.OnCellPainting(e);
        }

        private void PaintColumnHeader(System.Windows.Forms.DataGridViewCellPaintingEventArgs e, System.Drawing.Color color)
        {
            LinearGradientBrush backBrush = new LinearGradientBrush(new System.Drawing.Point(0, 0), new System.Drawing.Point(100, 100), color, color);
            e.Graphics.FillRectangle(backBrush, e.CellBounds);
            DataGridViewPaintParts parts = (DataGridViewPaintParts.All & ~DataGridViewPaintParts.Background);
            e.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.None;
            e.AdvancedBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.None;
            e.Paint(e.ClipBounds, parts);
            e.Handled = true;
        }
    }

这篇关于你如何突出显示DataGridView的行和/或列标签上(在C#)的任意单元格的鼠标悬停?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆