Redigeeri

Jagamisviis:


DataGridView.AreAllCellsSelected(Boolean) Method

Definition

Returns a value indicating whether all the DataGridView cells are currently selected.

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

Parameters

includeInvisibleCells
Boolean

true to include the rows and columns with Visible property values of false; otherwise, false.

Returns

true if all cells (or all visible cells) are selected or if there are no cells (or no visible cells); otherwise, false.

Examples

The following code example illustrates how to use this method to avoid calculations involving the 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

Remarks

The SelectedCells collection does not perform efficiently with large selections. To determine whether all the cells in the DataGridView have been selected before you access the contents of the SelectedCells collection, check the return value of the AreAllCellsSelected method. However, this method can cause rows to become unshared. For more information about DataGridView performance, see Best Practices for Scaling the Windows Forms DataGridView Control.

Applies to

See also