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

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

问题描述

使用 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<的大小/a>,您可能会看到闪烁,但这应该可以满足您的需求.

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;
        }
    }

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

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