DataGridView.RowPrePaint Evento
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Ocorre antes de a DataGridViewRow ser pintada.
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
Tipo de Evento
Exemplos
O exemplo de código seguinte demonstra como usar um handler para o RowPrePaint evento pintar um fundo de linha de gradiente se a linha for selecionada. Este exemplo faz parte de um exemplo maior disponível em Como: Personalizar a Aparência das Linhas no Controlo Windows Forms DataGridView.
// 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
Observações
Pode gerir este evento sozinho ou em combinação RowPostPaint para personalizar a aparência das linhas no controlo. Podes pintar carreiras inteiras tu próprio, ou pintar partes específicas de filas e usar os seguintes métodos da DataGridViewRowPrePaintEventArgs aula para pintar outras partes:
Também podes usar a VisualStyleRenderer classe para pintar os controlos padrão usando o tema atual. Para mais informações, consulte Controlos de Renderização com Estilos Visuais. Se estiver a usar Visual Studio 2005, também tem acesso a uma grande biblioteca de imagens padrão que pode usar com o controlo DataGridView.
Para mais informações sobre como gerir eventos, consulte Gestão e Levantamento de Eventos.