DataGridView.RowPrePaint Событие

Определение

Возникает перед DataGridViewRow краской.

public:
 event System::Windows::Forms::DataGridViewRowPrePaintEventHandler ^ RowPrePaint;
public event System.Windows.Forms.DataGridViewRowPrePaintEventHandler RowPrePaint;
public event System.Windows.Forms.DataGridViewRowPrePaintEventHandler? RowPrePaint;
member this.RowPrePaint : System.Windows.Forms.DataGridViewRowPrePaintEventHandler 
Public Custom Event RowPrePaint As DataGridViewRowPrePaintEventHandler 

Тип события

Примеры

В следующем примере кода показано, как использовать обработчик события для RowPrePaint рисования фона градиентной строки, если выбрана строка. Этот пример является частью более крупного примера, доступного в разделе "Практическое руководство. Настройка внешнего вида строк в элементе управления DataGridView в Windows Forms".

// Paints the custom selection background for selected rows.
void dataGridView1_RowPrePaint(object sender,
        DataGridViewRowPrePaintEventArgs e)
{
    // Do not automatically paint the focus rectangle.
    e.PaintParts &= ~DataGridViewPaintParts.Focus;

    // Determine whether the cell should be painted
    // with the custom selection background.
    if ((e.State & DataGridViewElementStates.Selected) ==
                DataGridViewElementStates.Selected)
    {
        // Calculate the bounds of the row.
        Rectangle rowBounds = new Rectangle(
            this.dataGridView1.RowHeadersWidth, e.RowBounds.Top,
            this.dataGridView1.Columns.GetColumnsWidth(
                DataGridViewElementStates.Visible) -
            this.dataGridView1.HorizontalScrollingOffset + 1,
            e.RowBounds.Height);

        // Paint the custom selection background.
        using (Brush backbrush =
            new System.Drawing.Drawing2D.LinearGradientBrush(rowBounds,
                this.dataGridView1.DefaultCellStyle.SelectionBackColor,
                e.InheritedRowStyle.ForeColor,
                System.Drawing.Drawing2D.LinearGradientMode.Horizontal))
        {
            e.Graphics.FillRectangle(backbrush, rowBounds);
        }
    }
}
' Paints the custom selection background for selected rows.
Sub dataGridView1_RowPrePaint(ByVal sender As Object, _
    ByVal e As DataGridViewRowPrePaintEventArgs) _
    Handles dataGridView1.RowPrePaint

    ' Do not automatically paint the focus rectangle.
    e.PaintParts = e.PaintParts And Not DataGridViewPaintParts.Focus

    ' Determine whether the cell should be painted with the 
    ' custom selection background.
    If (e.State And DataGridViewElementStates.Selected) = _
        DataGridViewElementStates.Selected Then

        ' Calculate the bounds of the row.
        Dim rowBounds As New Rectangle( _
            Me.dataGridView1.RowHeadersWidth, e.RowBounds.Top, _
            Me.dataGridView1.Columns.GetColumnsWidth( _
            DataGridViewElementStates.Visible) - _
            Me.dataGridView1.HorizontalScrollingOffset + 1, _
            e.RowBounds.Height)

        ' Paint the custom selection background.
        Dim backbrush As New _
            System.Drawing.Drawing2D.LinearGradientBrush(rowBounds, _
            Me.dataGridView1.DefaultCellStyle.SelectionBackColor, _
            e.InheritedRowStyle.ForeColor, _
            System.Drawing.Drawing2D.LinearGradientMode.Horizontal)
        Try
            e.Graphics.FillRectangle(backbrush, rowBounds)
        Finally
            backbrush.Dispose()
        End Try
    End If

End Sub

Комментарии

Это событие можно обрабатывать отдельно или в сочетании с RowPostPaint событием, чтобы настроить внешний вид строк в элементе управления. Вы можете самостоятельно рисовать целые строки или рисовать определенные части строк и использовать следующие методы класса для рисования DataGridViewRowPrePaintEventArgs других частей:

Можно также использовать VisualStyleRenderer класс для рисования стандартных элементов управления с помощью текущей темы. Дополнительные сведения см. в разделе "Элементы управления отрисовкой" с помощью визуальных стилей. Если вы используете Visual Studio 2005, у вас также есть доступ к большой библиотеке стандартных образов, которые можно использовать с элементом управления DataGridView.

Дополнительные сведения об обработке событий см. в разделе "Обработка и создание событий".

Применяется к

См. также раздел