DataGridViewColumnCollection.GetColumnsWidth Méthode

Définition

Retourne la largeur, en pixels, requise pour afficher toutes les colonnes qui répondent aux exigences du filtre donné.

public:
 int GetColumnsWidth(System::Windows::Forms::DataGridViewElementStates includeFilter);
public int GetColumnsWidth (System.Windows.Forms.DataGridViewElementStates includeFilter);
member this.GetColumnsWidth : System.Windows.Forms.DataGridViewElementStates -> int
Public Function GetColumnsWidth (includeFilter As DataGridViewElementStates) As Integer

Paramètres

includeFilter
DataGridViewElementStates

Combinaison d'opérations de bits des valeurs DataGridViewElementStates qui représentent le filtre à inclure.

Retours

La largeur, en pixels, nécessaire pour afficher toutes les colonnes qui répond aux exigences du filtre.

Exceptions

includeFilter n’est pas une combinaison d’opérations au niveau du bit de valeurs DataGridViewElementStates.

Exemples

L’exemple de code suivant illustre l’utilisation de cette méthode. Cet exemple fait partie d’un exemple plus large disponible dans Guide pratique pour personnaliser l’apparence des lignes dans le contrôle 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

S’applique à

Voir aussi