代码之家  ›  专栏  ›  技术社区  ›  Stuart Helwig

如何在任何单元格(c#)的鼠标上方突出显示datagridview的行和/或列标签?

  •  2
  • Stuart Helwig  · 技术社区  · 17 年前

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

    我希望在网格中的任何单元格上移动鼠标时产生这种效果,即高亮显示鼠标当前悬停的行的行标签。

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

    可以这样做吗?如果可以,怎么做?(最好是C)

    3 回复  |  直到 17 年前
        1
  •  9
  •   SwDevMan81    17 年前

    您可以覆盖 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), 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;
            }
        }
    
        2
  •  2
  •   BFree    17 年前

    您可以钩住DataGridView的CellMouseEnter和CellMouseLeave事件,然后相应地更改背景色。大概是这样的:

        private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0) //column header / row headers
            {
                return;
            } 
    
            this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.LightBlue;
        }
    
        private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0) //column header / row headers
            {
                return;
            }
    
            this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.White;
        }
    
        3
  •  1
  •   Lucas    9 年前

    我知道你对此已经有了回应,但我将分享一些不同的东西。

    这边 整排都漆好了 . 我只是稍微修改了@BFree注释。

            private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0) //column header / row headers
            {
                return;
            }
            foreach (DataGridViewCell cell in this.dataGridView1.Rows[e.RowIndex].Cells)
            {
               cell.Style.BackColor = Color.LightBlue;
            }
        }
    
        private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0) //column header / row headers
            {
                return;
            }
            foreach (DataGridViewCell cell in this.dataGridView1.Rows[e.RowIndex].Cells)
            {
                cell.Style.BackColor = Color.White;
            }
        }
    
    推荐文章