Ответ 1
Если вы хотите нарисовать границу вокруг выбранных строк, вы можете использовать DataGridView.RowPostPaintEvent и "очистить" цвета выбора, вы можете использовать DataGridViewCellStyle.SelectionBackColor и DataGridViewCellStyle.SelectionForeColor свойства.
Например, если я установил стиль ячейки строки, как этот
row.DefaultCellStyle.BackColor = Color.LightBlue;
row.DefaultCellStyle.SelectionBackColor = Color.LightBlue;
row.DefaultCellStyle.SelectionForeColor = dataGridView1.ForeColor;
Я могу добавить этот код в RowPostPaintEvent
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Selected)
{
using (Pen pen = new Pen(Color.Red))
{
int penWidth = 2;
pen.Width = penWidth;
int x = e.RowBounds.Left + (penWidth / 2);
int y = e.RowBounds.Top + (penWidth / 2);
int width = e.RowBounds.Width - penWidth;
int height = e.RowBounds.Height - penWidth;
e.Graphics.DrawRectangle(pen, x, y, width, height);
}
}
}
и выбранная строка будет выглядеть следующим образом: