DataGridViewRowPrePaintEventArgs.RowIndex Proprietà

Definizione

Ottiene l'indice del controllo DataGridViewRow corrente.

public:
 property int RowIndex { int get(); };
public int RowIndex { get; }
member this.RowIndex : int
Public ReadOnly Property RowIndex As Integer

Valore della proprietà

Indice dell'oggetto DataGridViewRow corrente.

Esempio

Nell'esempio di codice seguente viene illustrato come utilizzare la RowIndex proprietà per accedere alla riga corrente. Anche se il codice usa effettivamente la DataGridViewRowPostPaintEventArgs.RowIndex proprietà , questa proprietà è quasi identica alla RowIndex proprietà di DataGridViewRowPrePaintEventArgs. La variabile, e, è di tipo DataGridViewRowPostPaintEventArgs. Questo esempio di codice fa parte di un esempio più ampio fornito in Procedura: Personalizzare l'aspetto delle righe nel controllo DataGridView Windows Forms.

// Get the content that spans multiple columns.
object recipe =
    this.dataGridView1.Rows.SharedRow(e.RowIndex).Cells[2].Value;

if (recipe != null)
{
    String text = recipe.ToString();

    // Calculate the bounds for the content that spans multiple 
    // columns, adjusting for the horizontal scrolling position 
    // and the current row height, and displaying only whole
    // lines of text.
    Rectangle textArea = rowBounds;
    textArea.X -= this.dataGridView1.HorizontalScrollingOffset;
    textArea.Width += this.dataGridView1.HorizontalScrollingOffset;
    textArea.Y += rowBounds.Height - e.InheritedRowStyle.Padding.Bottom;
    textArea.Height -= rowBounds.Height -
        e.InheritedRowStyle.Padding.Bottom;
    textArea.Height = (textArea.Height / e.InheritedRowStyle.Font.Height) *
        e.InheritedRowStyle.Font.Height;

    // Calculate the portion of the text area that needs painting.
    RectangleF clip = textArea;
    clip.Width -= this.dataGridView1.RowHeadersWidth + 1 - clip.X;
    clip.X = this.dataGridView1.RowHeadersWidth + 1;
    RectangleF oldClip = e.Graphics.ClipBounds;
    e.Graphics.SetClip(clip);

    // Draw the content that spans multiple columns.
    e.Graphics.DrawString(
        text, e.InheritedRowStyle.Font, forebrush, textArea);

    e.Graphics.SetClip(oldClip);
}
' Get the content that spans multiple columns.
Dim recipe As Object = _
    Me.dataGridView1.Rows.SharedRow(e.RowIndex).Cells(2).Value

If (recipe IsNot Nothing) Then
    Dim text As String = recipe.ToString()

    ' Calculate the bounds for the content that spans multiple 
    ' columns, adjusting for the horizontal scrolling position 
    ' and the current row height, and displaying only whole
    ' lines of text.
    Dim textArea As Rectangle = rowBounds
    textArea.X -= Me.dataGridView1.HorizontalScrollingOffset
    textArea.Width += Me.dataGridView1.HorizontalScrollingOffset
    textArea.Y += rowBounds.Height - e.InheritedRowStyle.Padding.Bottom
    textArea.Height -= rowBounds.Height - e.InheritedRowStyle.Padding.Bottom
    textArea.Height = (textArea.Height \ e.InheritedRowStyle.Font.Height) * _
        e.InheritedRowStyle.Font.Height

    ' Calculate the portion of the text area that needs painting.
    Dim clip As RectangleF = textArea
    clip.Width -= Me.dataGridView1.RowHeadersWidth + 1 - clip.X
    clip.X = Me.dataGridView1.RowHeadersWidth + 1
    Dim oldClip As RectangleF = e.Graphics.ClipBounds
    e.Graphics.SetClip(clip)

    ' Draw the content that spans multiple columns.
    e.Graphics.DrawString(text, e.InheritedRowStyle.Font, forebrush, _
        textArea)

    e.Graphics.SetClip(oldClip)
End If

Commenti

L'indice di riga può essere utilizzato per accedere alla riga corrente all'interno di un oggetto DataGridViewRowCollection.

Si applica a

Vedi anche