DataGridView OnRowPostPaint draw

datagridview custom cell control
c# datagridview custom cell
c# datagridview custom column
c# datagridview change row color
bunifu datagridview
datagridview style examples
datagridview celltemplate c
c# winform datagridview

Actually, my code is "selecting hover" all the line, when i have the mouse over the cell, but, i want to change it, i want to select all the column, not the line.

Data Grid

How can i manage it?

My code is:

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

            }


        }
    }

I can find the same method, but to draw vertically...

Thank you

If You just want to select entire column while mouse is over any cell then You don't have to use OnRowPostPaint event , You can make the selection mode for the entire grid to select entire column instead of only one cell or entire row but to do that You need to make the SortMode for the Columns set to anything other than Automatic. here is how to do it:

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

then use either OnCellMouseEnter or OnCellMouseMove events to select whole column like this:

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

refer to this Answer for more information on selecting entire column.

Customize the Appearance of Rows in DataGridView Control , I think the problem is that if a row is originally set to red, when I change it to yellow, it restarts the the row post paint event. Private Sub� DataGridView OnRowPostPaint draw. Ask Question Asked 1 month ago. Active 1 month ago. Viewed 71 times 0. Actually, my code is "selecting hover" all the

Maybe not exactly what you want but maybe this example can get you started:

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

The example comes from this page docs.microsoft.com DataGridView.OnCellPainting

Datagridview Row Post Paint infinite loop issue, How to add row numbers to DataGridView numbers for DataGridView and DataGridViewX by handling RowPostPaint ControlText; // Draw row number e. If you are using Visual Studio 2005, you also have access to a large library of standard images that you can use with the DataGridView control. When handling this event, you should access the cell through the parameters of the event handler, rather than access the cell directly.

This will select all the cells within the hovered column... though it's not really clear what you are asking for.

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

here's an updated answer for just changing the color of the cells

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

How to add row numbers to DataGridView, If you have a data in a Windows Forms DataGridView, you may want to use some color to highlight specific rows of the grid to draw in the user's attention. In this example, the code RowPostPaint. If e.RowIndex = 2 Then The DataGridView control provides a customizable table for displaying data. The DataGridView class allows customization of cells, rows, columns, and borders through the use of properties such as DefaultCellStyle, ColumnHeadersDefaultCellStyle, CellBorderStyle, and GridColor.

Painting on the DataGridView -Deborah's Developer MindScape, //Draw the string Graphics g = dataGridView1.CreateGraphics(); g.Clip = new Region(e.RowBounds); g.RotateTransform(-45); g.DrawString(printMe, font, brush,� DataGridView can be slow not only when you add data to it, but even when the platform needs to repaint DataGridView. Slow DataGridView rendering can be fixed using so called double buffering. It is a technique used to draw a control’s contents to a temporary buffer and then copying the whole control image on the screen, which may greatly

Displaying a rotated string - DataGridView.RowPostPaint, Is DataGridView slow at scrolling, rendering, filtering or populating it with data? It is a technique used to draw a control's contents to a temporary buffer and� Establecer estilos de colores y fuentes en el control DataGridView de formularios Windows Forms How to: Set Font and Color Styles in the Windows Forms DataGridView Control. Cambiar el tipo de una columna DataGridView de Windows Forms mediante el Diseñador How to: Change the Type of a Windows Forms DataGridView Column Using the Designer

Why is DataGridView Slow at Scrolling, Rendering, Filtering and , In WinForms Applications, to display the row number in the row header, we could use the RowPostPaint event of DataGridView control. Usage:� protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e) { // this method overrides the DataGridView's RowPostPaint event // in order to automatically draw numbers on the row header cells // and to automatically adjust the width of the column containing // the row header cells so that it can accommodate the new row // numbers, // store a string representation of the row number in 'strRowNumber' string strRowNumber = (e.RowIndex + 1).ToString(); // prepend leading zeros to

Comments
  • on mouseover you want to select all the cells in the column being hovered? Is that it?
  • yes! instead of selecting all the row, i want all the cells in the column
  • actually i dont want to "select" the cells, only "hover" the cells in the column with another color...
  • you can check the updated answer of @blaze_125 , I tested it and it behaves just like what you asked and also is very fast in rendering , you should mark his answer as answered otherwise add clarification in comments of what you need to do.
  • actually i dont want to "select" the cells, only "hover" the cells in the column with another color...