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

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

问题描述

使用Windows窗体上的DataGridView控件,当您将鼠标移动到行标签(或列标签)上时,它(标签单元格)的背景将变为蓝色(或其他颜色取决于Windows配色方案)怀疑)。



我想在鼠标移动到网格中的任何单元格时产生这种效果 - 即突出显示鼠标当前盘旋的行的行标签。



使用mouseover事件,更改当前行的样式的逻辑是足够简单的。我可以改变行的其他属性(比如说背景颜色),但我真的会喜欢比这更微妙的东西,我认为这个行标签的突出显示将是非常有效的。



<可以做到吗 - 如果是这样做? (C#最好)

解决方案

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

  
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),新的System.Drawing.Point(100,100),颜色,颜色);
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;
}
}


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.

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

解决方案

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天全站免登陆