共用方式為


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

true 表示加入 Visible 屬性值為 false 的資料列和資料行;否則為 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 不會透過大型選取專案有效率地執行。 若要判斷 中 DataGridView 是否已選取 的所有儲存格,再存取集合的內容 SelectedCells ,請檢查 方法的 AreAllCellsSelected 傳回值。 不過,此方法可能會導致數據列變成未共用。 如需效能的詳細資訊DataGridView,請參閱調整 dataGridView 控件 Windows Forms 最佳做法

適用於

另請參閱