DataGridView.GetCellCount(DataGridViewElementStates) 方法

定義

取得滿足提供的篩選條件的儲存格數目。

public:
 int GetCellCount(System::Windows::Forms::DataGridViewElementStates includeFilter);
public int GetCellCount (System.Windows.Forms.DataGridViewElementStates includeFilter);
member this.GetCellCount : System.Windows.Forms.DataGridViewElementStates -> int
Public Function GetCellCount (includeFilter As DataGridViewElementStates) As Integer

參數

includeFilter
DataGridViewElementStates

DataGridViewElementStates 值的位元 (Bitwise) 組合,可指定所要計數的儲存格。

傳回

Int32

符合 includeFilter 參數的儲存格數目。

例外狀況

includeFilter 包含 ResizableSet 值。

範例

下列程式碼範例說明如何使用這個方法來判斷控制項中 DataGridView 是否選取任何儲存格。 在此範例中,如果選取任何儲存格,則會透過 方法擷取其值, GetClipboardContent 並顯示在 控制項中 TextBox

此程式碼是較大型範例的一部分,說明如何使用控制項的 DataGridView 剪貼簿功能。 此範例是 How to: Enable Users to Copy Multiple Cells to the Clipboard from the Windows Forms DataGridView Control中較大型範例的一部分。

private void CopyPasteButton_Click(object sender, System.EventArgs e)
{
    if (this.DataGridView1
        .GetCellCount(DataGridViewElementStates.Selected) > 0)
    {
        try
        {
            // Add the selection to the clipboard.
            Clipboard.SetDataObject(
                this.DataGridView1.GetClipboardContent());
            
            // Replace the text box contents with the clipboard text.
            this.TextBox1.Text = Clipboard.GetText();
        }
        catch (System.Runtime.InteropServices.ExternalException)
        {
            this.TextBox1.Text = 
                "The Clipboard could not be accessed. Please try again.";
        }
    }
}
Private Sub CopyPasteButton_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles CopyPasteButton.Click

    If Me.DataGridView1.GetCellCount( _
        DataGridViewElementStates.Selected) > 0 Then

        Try

            ' Add the selection to the clipboard.
            Clipboard.SetDataObject( _
                Me.DataGridView1.GetClipboardContent())

            ' Replace the text box contents with the clipboard text.
            Me.TextBox1.Text = Clipboard.GetText()

        Catch ex As System.Runtime.InteropServices.ExternalException
            Me.TextBox1.Text = _
                "The Clipboard could not be accessed. Please try again."
        End Try

    End If

End Sub

備註

這個方法有助於判斷處於特定狀態的儲存格數目。 例如,若要擷取選取的儲存格數目,請使用這個方法搭配 DataGridViewElementStates.Selected 值。 這通常比使用 SelectedCells 屬性更有效率。

適用於

另請參閱