DataGridView.GetCellCount(DataGridViewElementStates) Método

Definición

Obtiene el número de celdas que satisfacen el filtro proporcionado.

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

Parámetros

includeFilter
DataGridViewElementStates

Combinación bit a bit de los valores de DataGridViewElementStates que especifica las celdas que se van a contar.

Devoluciones

Int32

Número de celdas que coinciden con el parámetro includeFilter.

Excepciones

includeFilter incluye el valor ResizableSet.

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar este método para determinar si hay celdas seleccionadas en un DataGridView control. En este ejemplo, si se seleccionan celdas, sus valores se recuperan a través del GetClipboardContent método y se muestran en un TextBox control .

Este código forma parte de un ejemplo más grande que ilustra el uso de las características del Portapapeles del DataGridView control. Este ejemplo forma parte de un ejemplo más grande disponible en 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

Comentarios

Este método es útil para determinar el número de celdas en un estado determinado. Para recuperar el número de celdas seleccionadas, por ejemplo, use este método con el DataGridViewElementStates.Selected valor . Normalmente, esto es más eficaz que usar la SelectedCells propiedad .

Se aplica a

Consulte también