Remove all rows from datagridview except column header

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 2 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
DanielLeber
Posts: 20
Last visit: Thu May 18, 2023 8:36 am

Remove all rows from datagridview except column header

Post by DanielLeber »

Product: PowerShell Studio 2021 (64 Bit)
Build: v5.8.194
OS: Windows 10 Education (64 Bit)
Build: v10.0.19041.0

Hello,
how can all rows be removed from a datagridview, without the column header? $dataGridView.DataSource = $null remove the header too
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Remove all rows from datagridview except column header

Post by jvierra »

Just "Clear()" the "Rows[]" collection.
User avatar
DanielLeber
Posts: 20
Last visit: Thu May 18, 2023 8:36 am

Re: Remove all rows from datagridview except column header

Post by DanielLeber »

The $dataGridView.Rows.Clear() not really works. Exception calling "Clear" with "0" argument(s): "Cannot clear this list." Do you have another idea?
Attachments
29-09-_2021_20-00-55.png
29-09-_2021_20-00-55.png (50.1 KiB) Viewed 6191 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Remove all rows from datagridview except column header

Post by jvierra »

If you are using a datatable then you have to clear the rows in the datatable object and not the grid rows.
User avatar
DanielLeber
Posts: 20
Last visit: Thu May 18, 2023 8:36 am

Re: Remove all rows from datagridview except column header

Post by DanielLeber »

Inside the ConvertTo-DataTable function is a method, to clear the datatable object
30-09-_2021_08-35-01.png
30-09-_2021_08-35-01.png (26.1 KiB) Viewed 6165 times
I would like to clear all gridview entries when the ComboBox index changes, but keep the header. How can I realize this?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Remove all rows from datagridview except column header

Post by jvierra »

No. You have to clear the DataTable object with is the object attached to the DataSource property. Of course, that depends on how you created the DataGridView and loaded it. There are many ways to load a DataGridView control.
User avatar
DanielLeber
Posts: 20
Last visit: Thu May 18, 2023 8:36 am

Re: Remove all rows from datagridview except column header

Post by DanielLeber »

I load the DataGridView control as a Job.
30-09-_2021_10-39-36.png
30-09-_2021_10-39-36.png (76.93 KiB) Viewed 6162 times
Have you an example for me how can I clear it from the $ComboBoxPrintServer_SelectedIndexChanged = {} ?

I'm kind of at a loss here
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Remove all rows from datagridview except column header

Post by jvierra »

That's easy, just clear it. Remember the datatable should not be assigned via an Update-DataGridView as that would be redundant. Just assign it as the DataSource.
User avatar
DanielLeber
Posts: 20
Last visit: Thu May 18, 2023 8:36 am

Re: Remove all rows from datagridview except column header

Post by DanielLeber »

It's easy when you know how it works. Please, give me a specific example
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Remove all rows from datagridview except column header

Post by jvierra »

Just "Clear()" the DataSource. Why is that so hard? You have a table referenced by the DataSource so just call "Clear()" on the referenced object through the DataSource reference.
This topic is 2 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked