DataGridView.AreAllCellsSelected(Boolean) Méthode

Définition

Retourne une valeur indiquant si toutes les cellules DataGridView sont actuellement sélectionnées.

public:
 bool AreAllCellsSelected(bool includeInvisibleCells);
public bool AreAllCellsSelected (bool includeInvisibleCells);
member this.AreAllCellsSelected : bool -> bool
Public Function AreAllCellsSelected (includeInvisibleCells As Boolean) As Boolean

Paramètres

includeInvisibleCells
Boolean

true pour inclure les lignes et les colonnes avec des valeurs de propriété Visible de false ; sinon, false.

Retours

true si toutes les cellules (ou toutes les cellules visibles) sont sélectionnées ou s'il n'y a aucune cellule (ou aucune cellule visible) ; sinon, false.

Exemples

L’exemple de code suivant montre comment utiliser cette méthode pour éviter les calculs impliquant la SelectedCells collection.

private void selectedCellsButton_Click(object sender, System.EventArgs e)
{
    Int32 selectedCellCount =
        dataGridView1.GetCellCount(DataGridViewElementStates.Selected);
    if (selectedCellCount > 0)
    {
        if (dataGridView1.AreAllCellsSelected(true))
        {
            MessageBox.Show("All cells are selected", "Selected Cells");
        }
        else
        {
            System.Text.StringBuilder sb =
                new System.Text.StringBuilder();

            for (int i = 0;
                i < selectedCellCount; i++)
            {
                sb.Append("Row: ");
                sb.Append(dataGridView1.SelectedCells[i].RowIndex
                    .ToString());
                sb.Append(", Column: ");
                sb.Append(dataGridView1.SelectedCells[i].ColumnIndex
                    .ToString());
                sb.Append(Environment.NewLine);
            }

            sb.Append("Total: " + selectedCellCount.ToString());
            MessageBox.Show(sb.ToString(), "Selected Cells");
        }
    }
}
Private Sub selectedCellsButton_Click( _
    ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles selectedCellsButton.Click

    Dim selectedCellCount As Integer = _
        dataGridView1.GetCellCount(DataGridViewElementStates.Selected)

    If selectedCellCount > 0 Then

        If dataGridView1.AreAllCellsSelected(True) Then

            MessageBox.Show("All cells are selected", "Selected Cells")

        Else

            Dim sb As New System.Text.StringBuilder()

            Dim i As Integer
            For i = 0 To selectedCellCount - 1

                sb.Append("Row: ")
                sb.Append(dataGridView1.SelectedCells(i).RowIndex _
                    .ToString())
                sb.Append(", Column: ")
                sb.Append(dataGridView1.SelectedCells(i).ColumnIndex _
                    .ToString())
                sb.Append(Environment.NewLine)

            Next i

            sb.Append("Total: " + selectedCellCount.ToString())
            MessageBox.Show(sb.ToString(), "Selected Cells")

        End If

    End If

End Sub

Remarques

La SelectedCells collection ne fonctionne pas efficacement avec des sélections volumineuses. Pour déterminer si toutes les cellules du DataGridView ont été sélectionnées avant d’accéder au contenu de la SelectedCells collection, case activée la valeur de retour de la AreAllCellsSelected méthode . Toutefois, cette méthode peut entraîner le non-partage des lignes. Pour plus d’informations sur DataGridView les performances, consultez Meilleures pratiques pour la mise à l’échelle du contrôle DataGridView Windows Forms.

S’applique à

Voir aussi