Language

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 値のビットごとの組み合わせ。

返品

includeFilter パラメーターに一致するセルの数。

例外

includeFilter には値 ResizableSetが含まれます。

例

次のコード例は、このメソッドを使用して、 DataGridView コントロールで選択されたセルがあるかどうかを判断する方法を示しています。 この例では、セルが選択されている場合、その値は GetClipboardContent メソッドを使用して取得され、 TextBox コントロールに表示されます。

このコードは、 DataGridView コントロールのクリップボード機能の使用を示す、より大きな例の一部です。 この例は、「 方法: ユーザーが Windows フォーム DataGridView コントロールからクリップボードに複数のセルをコピーできるようにする」で使用できる、より大きな例の一部です。

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 プロパティを使用するよりも効率的です。

適用対象

こちらもご覧ください