DataGridView.AreAllCellsSelected(Boolean) 方法

定义

返回一个值,该值指示当前是否选择了所有的 DataGridView 单元格。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

参数

includeInvisibleCells
Boolean

如果包括 Visible 属性值为 false 的行和列,则为 true;否则为 falsetrue to include the rows and columns with Visible property values of false; otherwise, false.

返回

如果选择了所有的单元格(或所有可见的单元格)或者没有单元格(或没有可见的单元格),则为 true;否则为 falsetrue if all cells (or all visible cells) are selected or if there are no cells (or no visible cells); otherwise, false.

示例

下面的代码示例演示如何使用此方法来避免涉及到 @no__t 集合的计算。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

注解

@No__t 的集合不会通过大型选择有效地执行。The SelectedCells collection does not perform efficiently with large selections. 若要确定在访问 @no__t 集合的内容之前是否选择了 DataGridView 中的所有单元格,请检查 @no__t 方法的返回值。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. 有关 @no__t 0 性能的详细信息,请参阅缩放 Windows 窗体 DataGridView 控件的最佳做法For more information about DataGridView performance, see Best Practices for Scaling the Windows Forms DataGridView Control.

适用于

另请参阅