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