Search code examples
powershelldatagridviewvisiblerowcount

Count the visible rows in DataGridView using Powershell


After I've used $DataGridView1.Rows[$row.Index].Visible = $false to hide some rows, I need to count how many are visible now.

If I use $DataGridView1.Rows.GetRowCount.ToString()

I get this result:

int GetRowCount(System.Windows.Forms.DataGridViewElementStates includeFilter)

So, if I use $DataGridView1.Rows.GetRowCount($DataGridViewElementStates.Visible)

I would expect a number of the visible rows in my DataGridView, but it returns an exception:

    Cannot convert argument "includeFilter", with value: "", for "GetRowCount" 
to type "System.Windows.Forms.DataGridViewElementStates": "Cannot convert null 
to type "System.Windows.Forms.DataGridViewElementStates" due to enumeration 
values that are not valid. Specify one of the following enumeration values and 
try again. The possible enumeration values are 
"None,Displayed,Frozen,ReadOnly,Resizable,ResizableSet,Selected,Visible"."

What am I doing wrong?


Solution

  • As the error indicates, you'll need to supply a value of type System.Windows.Forms.DataGridViewElementStates:

    $DataGridView1.Rows.GetRowCount([System.Windows.Forms.DataGridViewElementStates]::Visible)