locked
Draw Rectangle or Bold Border on selected Cell in DataGridView RRS feed

  • Question

  •  

    Hi,

     

    I want to ask how to draw rectangle or bold border on selected cell in datagridview.

    But i want it when its enter the cell, when leave the cell the rectangle move to the selected cells.

    Example: Excel grid, when we focus to 1 cell it will draw rectangle without highlighting the cell.

     

    thax

    Friday, May 23, 2008 7:23 AM

Answers

  • You can handle the CellPainting event to paint the border by yourself, and handle the CellEnter and CellLeave to force the DataGridView to redraw.

    private void Form6_Load(object sender, EventArgs e)

            {

                DataTable dt = new DataTable();

                dt.Columns.Add("c1");

                dt.Columns.Add("c2");

                dt.Columns.Add("c3");

                for (int j = 0; j < 10; j++)

                {

                    dt.Rows.Add("aa","bbvb","ss");

                }

     

                this.dataGridView1.DataSource = dt;

                this.dataGridView1.DefaultCellStyle.SelectionBackColor = Color.White;

                this.dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black;

            }

     

            void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)

            {

                this.dataGridView1.Invalidate();

            }

     

            void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)

            {

                this.dataGridView1.Invalidate();

            }

     

            void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

            {

                if (e.ColumnIndex == this.dataGridView1.CurrentCell.ColumnIndex

                    && e.RowIndex == this.dataGridView1.CurrentCell.RowIndex)

                {

                    e.Paint(e.CellBounds, DataGridViewPaintParts.All
     &~ DataGridViewPaintParts.Border);

                    using (Pen p = new Pen(Color.Black, 4))

                    {

                        Rectangle rect = e.CellBounds;

                        rect.Width -= 2;

                        rect.Height -= 2;

                        e.Graphics.DrawRectangle(p, rect);

                    }

                    e.Handled = true;

                }

            }

     

    Tuesday, May 27, 2008 2:03 PM

All replies

  • You can handle the CellPainting event to paint the border by yourself, and handle the CellEnter and CellLeave to force the DataGridView to redraw.

    private void Form6_Load(object sender, EventArgs e)

            {

                DataTable dt = new DataTable();

                dt.Columns.Add("c1");

                dt.Columns.Add("c2");

                dt.Columns.Add("c3");

                for (int j = 0; j < 10; j++)

                {

                    dt.Rows.Add("aa","bbvb","ss");

                }

     

                this.dataGridView1.DataSource = dt;

                this.dataGridView1.DefaultCellStyle.SelectionBackColor = Color.White;

                this.dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black;

            }

     

            void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)

            {

                this.dataGridView1.Invalidate();

            }

     

            void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)

            {

                this.dataGridView1.Invalidate();

            }

     

            void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

            {

                if (e.ColumnIndex == this.dataGridView1.CurrentCell.ColumnIndex

                    && e.RowIndex == this.dataGridView1.CurrentCell.RowIndex)

                {

                    e.Paint(e.CellBounds, DataGridViewPaintParts.All
     &~ DataGridViewPaintParts.Border);

                    using (Pen p = new Pen(Color.Black, 4))

                    {

                        Rectangle rect = e.CellBounds;

                        rect.Width -= 2;

                        rect.Height -= 2;

                        e.Graphics.DrawRectangle(p, rect);

                    }

                    e.Handled = true;

                }

            }

     

    Tuesday, May 27, 2008 2:03 PM
  • Thanks for the tip on capturing the CellPainting event. I've been trying to remove the focus border from cells in my datagridview for a week (since i use row select and it ruins the look) and this worked:

     

    Code Snippet

    private void dgv_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

    {

    e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.Focus);

    e.Handled = true;

    }

     

     

    Tuesday, May 27, 2008 4:13 PM