У меня есть Datagridview. Мне нужно использовать событие CellPainting, чтобы настроить внешний вид моего DataGridview. Я использовал код документации msd:
Все работает отлично, я смог настроить свой Datagridview так, как мне нужно. Но единственное, что больше не работает, это выбор строки: ячейки, в которых событие рисования выполнило свою работу, исключаются из выбора. Это выглядит так:
Кто-нибудь знает, что мне нужно сделать, чтобы выбор снова работал нормально?
Редактировать: я также пытался повторно использовать Cellstyle с помощью «ApplyStyle». Не работает.
Edit2: Вот код из MSD. Его применение приведет к тому, что выбор не будет работать должным образом.
private void dataGridView1_CellPainting(object sender,
System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
{
if (this.dataGridView1.Columns["ContactName"].Index ==
e.ColumnIndex && e.RowIndex >= 0)
{
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;
}
}
}
}
Вы имеете в виду backColorBrush = new SolidBrush((e.State & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected ? e.CellStyle.SelectionBackColor : e.CellStyle.BackColor ?





из dr.null я построил следующий код:
// checks if cell is selected. if so, paint the background with the selectionbackgroundcolor.
if ((e.State & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected)
{
var selectedbrush = new SolidBrush(e.CellStyle.SelectionBackColor);
e.Graphics.FillRectangle(selectedbrush, e.CellBounds);
}
этот код находится в CellPaintingEvent.
:
Предоставьте достаточно кода, чтобы другие могли лучше понять или воспроизвести проблему.