Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

DataGridView Select Cell an Row Color mit dem Paint-Events

$
0
0

on click a cell in dgv i will paint a new background on the complete row and

a boarder on the clicked cell, but this code has en error

private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            if  (e.RowIndex != -1)
            {
                if (e.RowIndex == this.dataGridView1.CurrentCell.RowIndex)
                {
                    e.PaintCells(e.RowBounds, DataGridViewPaintParts.All);
                    Rectangle rect = e.RowBounds;
                    Brush colorBrush = new SolidBrush(Color.Red);
                    e.Graphics.FillRectangle(colorBrush, rect);

                    e.Handled = true;

                }
            }
        }

        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                e.Paint(e.CellBounds, DataGridViewPaintParts.All & DataGridViewPaintParts.Border);

                if (e.RowIndex == this.dataGridView1.CurrentCell.RowIndex)
                {
                    if (e.ColumnIndex == this.dataGridView1.CurrentCell.ColumnIndex)
                    {
                        if (e.ColumnIndex == 4 || e.ColumnIndex == 5 || e.ColumnIndex == 6 || e.ColumnIndex == 8 || e.ColumnIndex == 9)
                        {
                            using (Pen p = new Pen(Color.Black, 3))
                            {
                                this.DoubleBuffered = true;
                                Rectangle rec = e.CellBounds;
                                rec.Width -= 2;
                                rec.Height -= 2;
                                e.Graphics.DrawRectangle(p, rec);
                            }
                        }
                        e.Handled = true;
                    }
                }
            }
        }

        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            this.dataGridView1.Invalidate();
        }

        private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            this.dataGridView1.Invalidate();
        }



Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>