DataGridView.GetCellCount(DataGridViewElementStates) DataGridView.GetCellCount(DataGridViewElementStates) DataGridView.GetCellCount(DataGridViewElementStates) DataGridView.GetCellCount(DataGridViewElementStates) Method


Gets the number of cells that satisfy the provided filter.

 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


DataGridViewElementStates DataGridViewElementStates DataGridViewElementStates DataGridViewElementStates

A bitwise combination of the DataGridViewElementStates values specifying the cells to count.


The number of cells that match the includeFilter parameter.



The following code example illustrates how to use this method to determine whether there are any cells selected in a DataGridView control. In this example, if any cells are selected, their values are retrieved through the GetClipboardContent method and displayed in a TextBox control.

This code is part of a larger example illustrating the use of the Clipboard features of the DataGridView control. This example is part of a larger example available in 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)
            // Add the selection to the clipboard.
            // 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


            ' Add the selection to the clipboard.
            Clipboard.SetDataObject( _

            ' 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


This method is useful to determine the number of cells in a particular state. To retrieve the number of selected cells, for example, use this method with the DataGridViewElementStates.Selected value. This is typically more efficient than using the SelectedCells property.

Applies to

See also