【问题标题】:DataGridView OnRowPostPaint drawDataGridView OnRowPostPaint 绘制
【发布时间】:2019-11-15 03:11:51
【问题描述】:

实际上,我的代码是“选择悬停”所有行,当我将鼠标悬停在单元格上时,但是,我想更改它,我想选择所有 ,而不是行。

Data Grid

我该如何管理它?

我的代码是:

    protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
    {
        base.OnRowPostPaint(e);
        if (this.RectangleToScreen(e.RowBounds).Contains(MousePosition))
        {
            using (var b = new SolidBrush(Color.FromArgb(50, Color.Black)))
            using (var p = new Pen(Color.MediumVioletRed))
            {
                var r = e.RowBounds;
                r.Width -= 1;
                r.Height -= 1;

                e.Graphics.FillRectangle(b, e.RowBounds.X, e.RowBounds.Y,e.RowBounds.Width,  e.RowBounds.Height);
                e.Graphics.DrawRectangle(p, r);

            }


        }
    }

我可以找到相同的方法,但要垂直绘制...

谢谢

【问题讨论】:

  • 鼠标悬停时您想选择悬停列中的所有单元格吗?是这样吗?
  • 是的!而不是选择所有行,我想要列中的所有单元格

标签: c# winforms datagridview


【解决方案1】:

如果您只想在鼠标悬停在任何单元格上时选择整列,则不必使用 OnRowPostPaint 事件,您可以使整个网格的选择模式选择整列而不是仅选择一个单元格或整行但要做到这一点,您需要将列的 SortMode 设置为自动以外的任何值。这是怎么做的:

private void Form1_Load(object sender, EventArgs e)
{
  for (int i = 0; i < dataGridView1.ColumnCount; i++)
  {
    dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
  }
  dataGridView1.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;
}

然后使用 OnCellMouseEnter 或 OnCellMouseMove 事件来选择整个列,如下所示:

private void OnCellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
{
  if (e.RowIndex == -1 || e.ColumnIndex == -1)
    return;
  dataGridView1.CurrentCell = dataGridView1[e.ColumnIndex, e.RowIndex];
}

有关选择整列的更多信息,请参阅此Answer

【讨论】:

  • 其实我不想“选择”单元格,只是用另一种颜色“悬停”列中的单元格...
  • 您可以查看@blaze_125 的更新答案,我对其进行了测试,它的行为与您所要求的一样,而且渲染速度也非常快,您应该将他的答案标记为已回答,否则请在 cmets 中添加说明你需要做什么。
【解决方案2】:

也许不是你想要的,但也许这个例子可以让你开始:

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {

        Rectangle newRect = new Rectangle(e.CellBounds.X + 1,
            e.CellBounds.Y + 1, e.CellBounds.Width - 4,
            e.CellBounds.Height - 4);

        using (
            Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
            backColorBrush = new SolidBrush(e.CellStyle.BackColor))
        {
            using (Pen gridLinePen = new Pen(gridBrush))
            {
                // Erase the cell.
                e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

                // Draw the grid lines (only the right and bottom lines;
                // DataGridView takes care of the others).
                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
                    e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
                    e.CellBounds.Bottom - 1);
                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
                    e.CellBounds.Top, e.CellBounds.Right - 1,
                    e.CellBounds.Bottom);

                // Draw the inset highlight box.
                e.Graphics.DrawRectangle(Pens.Blue, newRect);

                // Draw the text content of the cell, ignoring alignment.
                if (e.Value != null)
                {
                    e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
                        Brushes.Crimson, e.CellBounds.X + 2,
                        e.CellBounds.Y + 2, StringFormat.GenericDefault);
                }
                e.Handled = true;
            }
        }
    }

示例来自本页docs.microsoft.com DataGridView.OnCellPainting

【讨论】:

    【解决方案3】:

    这将选择悬停列中的所有单元格...尽管您的要求并不清楚。

    using System.Windows.Forms;
    
    namespace DatagridViewSelectAllCells_58842788
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                dataGridView1.Columns.Add(new DataGridViewTextBoxColumn());
                dataGridView1.Columns.Add(new DataGridViewTextBoxColumn());
                for (int i = 0; i < 10; i++)
                {
                    dataGridView1.Rows.Add($"col1_{i}", $"col2_{i}");
                }
                dataGridView1.CellMouseEnter += DataGridView1_CellMouseEnter;
            }
    
            private void DataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex == -1)//don't do anything if the rowheader is being hovered
                {
                    return;
                }
                if (dataGridView1.SelectedCells.Count > 1 && e.ColumnIndex == dataGridView1.SelectedCells[0].ColumnIndex)//don't do anything if the column is already selected
                {
                    return;
                }
                dataGridView1.ClearSelection();//clear the current selection
    
                //select all the cells in that hovered column
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    dataGridView1[e.ColumnIndex, i].Selected = true;
                }
            }
        }
    }
    

    这里是更改单元格颜色的更新答案

    using System.Windows.Forms;
    using System.Drawing;
    
    namespace DatagridViewSelectAllCells_58842788
    {
        public partial class Form1 : Form
        {
            static Color originalColor;
            static int highlightedCol = -1;
            static DataGridView dataGridView1 = new DataGridView();
            public Form1()
            {
                InitializeComponent();
                Controls.Add(dataGridView1);
                dataGridView1.Dock = DockStyle.Fill;
                dataGridView1.Columns.Add(new DataGridViewTextBoxColumn());
                dataGridView1.Columns.Add(new DataGridViewTextBoxColumn());
                for (int i = 0; i < 10; i++)
                {
                    dataGridView1.Rows.Add($"col1_{i}", $"col2_{i}");
                }
                dataGridView1.CellMouseEnter += DataGridView1_CellMouseEnter;
                dataGridView1.CellMouseLeave += DataGridView1_CellMouseLeave;
    
                dataGridView1[1, 4].Style.BackColor = Color.DarkOliveGreen;
            }
    
            private void DataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
            {
                //WithCellStyle(sender, e, "Leave");
                WithDefaultStyle(sender, e, "Leave");
            }
    
            private void DataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
            {
                //WithCellStyle(sender, e, "Enter");
                WithDefaultStyle(sender, e, "Enter");
            }
    
    
            /*
             * to handle cells that have specific style attributes
             * like cell dataGridView1[1, 4]
             * there would be more to this to keep track of multiple different cell styles on the Leave side of thing
             * but that's up to you to handle if you have a need for it.
             */
            private static void WithCellStyle(object sender, DataGridViewCellEventArgs e, string eType)
            {
                switch (eType)
                {
                    case "Enter":
                        if (originalColor == null)
                        {
                            originalColor = ((Control)sender).BackColor;//store the color for leave event
                        }
                        highlightedCol = e.ColumnIndex;//set which column is highlighted for leave event
    
                        for (int i = 0; i < dataGridView1.Rows.Count; i++)
                        {
                            dataGridView1[highlightedCol, i].Style.BackColor = Color.Aquamarine;
                        }
                        break;
                    case "Leave":
                        for (int i = 0; i < dataGridView1.Rows.Count; i++)
                        {
                            dataGridView1[highlightedCol, i].Style.BackColor = originalColor;
                        }
                        break;
                    default:
                        break;
                }
            }
    
            /*
             * if you only have default styles
             */
            private static void WithDefaultStyle(object sender, DataGridViewCellEventArgs e, string eType)
            {
                switch (eType)
                {
                    case "Enter":
                        if (originalColor == null)
                        {
                            originalColor = ((Control)sender).BackColor;//store the color for leave event
                        }
                        highlightedCol = e.ColumnIndex;//set which column is highlighted for leave event
    
                        //change color of all the cells
                        dataGridView1.Columns[highlightedCol].DefaultCellStyle.BackColor = Color.Aquamarine;//set the new color
                        break;
                    case "Leave":
                        //per defaultstyle
                        dataGridView1.Columns[highlightedCol].DefaultCellStyle.BackColor = originalColor;
                        break;
                    default:
                        break;
                }
            }
        }
    }
    

    【讨论】:

    • 其实我不想“选择”单元格,只是用另一种颜色“悬停”列中的单元格...
    猜你喜欢
    • 2010-09-20
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多