DataGridView.AreAllCellsSelected(Boolean) 方法

定义

返回一个值,该值指示当前是否选择了所有的 DataGridView 单元格。

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

参数

includeInvisibleCells
Boolean

如果包括 Visible 属性值为 false 的行和列,则为 true;否则为 false

返回

如果选择了所有的单元格(或所有可见的单元格)或者没有单元格(或没有可见的单元格),则为 true;否则为 false

示例

下面的代码示例演示如何使用此方法来避免涉及集合的 SelectedCells 计算。

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

注解

在选定了大量单元格的情况下,SelectedCells 集合无法高效执行。 若要在访问 SelectedCells 集合的内容之前确定是否已选择 DataGridView 中的所有单元格,请检查 AreAllCellsSelected 方法的返回值。 但是,此方法可能会导致行变得非共享。 有关性能的详细信息DataGridView,请参阅缩放 Windows 窗体 DataGridView 控件的最佳做法

适用于

另请参阅