Datagridview | Select DataBoundItem

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.
Locked
User avatar
rubberduck
Posts: 62
Last visit: Tue Jul 06, 2021 5:13 am

Datagridview | Select DataBoundItem

Post by rubberduck »

PowerShell Studio 2018 v5.5.150 @ Windows 10 Pro x64


Hello,

I have a simple'Windows Forms' application,
which displays computer objects in a'Datagridview' ('Datatable').

This works very well.
To sort the view by active and inactive computers, I have two checkboxes .

But when I update the Datagridview again, I suddenly have columns I haven't seen before.
e.g. "DataView" "RowVersion" "IsNew" etc.
Since I export the view of the Datagrid later as CSV, I don't want to have these columns with me.
How can I prevent them from being displayed?
Maybe I don't read the view of the datagrid correctly either?

Enclosed the code, an image of the GUI and the exported Excel csv, then it will surely be clearer.
The code of the checkbox is:
  1. if ($checkboxDisabled.CheckState -eq 'Checked')
  2.     {$DT = ConvertTo-DataTable -InputObject ($datagridview.Rows | Select-Object -ExpandProperty DataBoundItem)
  3. $DT.DefaultView.RowFilter = "Active like '%no%'"
  4. Update-DataGridView -DataGridView $datagridview -Item $DT #-AutoSizeColumns AllCells
  5. $datagridview.Refresh()
  6. $countFiltered.Invoke()
  7. }
GUI.jpg
GUI.jpg (51.06 KiB) Viewed 2161 times
Excel Export.jpg
Excel Export.jpg (86.35 KiB) Viewed 2161 times

jvierra
Posts: 14867
Last visit: Fri Jul 23, 2021 7:52 pm
Answers: 12
Has voted: 3 times
Been upvoted: 10 times

Re: Datagridview | Select DataBoundItem

Post by jvierra »

You cannot manipulate a DataGridView that way. You can export the grid, filter the grid or sort the grid. You cannot reconvert the grid data to a new table.

A loaded data table is automatically sortable by clicking the column header. To filter just set the RowFilter property of the DefaultView. Once filtered the filtered grid can be exported without further manipulation.

User avatar
rubberduck
Posts: 62
Last visit: Tue Jul 06, 2021 5:13 am

Re: Datagridview | Select DataBoundItem

Post by rubberduck »

Okay, thanks for your answer.

All in all, I would like to achieve the following.
Read data from AD, convert to data table, show in grid view ( works)
When filtering, filter the Datagridview and show it again (works)
If the filter is deactivated, display the original DataTable again.

Somehow I have to save the original state into a variable, so I can switch between filtered view and original view back and forth.

I thought I had to convert the current view of my DatagridView back to a datatable. Ok, you say you don't do that, but how do I save the current (filtered )view of my datagrid view to continue working with it (export csv) and how do i switch back when the filter is off again?.


My export function is:
  1. $btnCreateCSV_Click = {
  2.     $SelectedRows = $datagridview.SelectedRows
  3.     $SelectedRows | Select-Object -ExpandProperty DataBoundItem | Export-Csv C:\$temp\Export.csv -NoTypeInformation
  4.         Invoke-Item C:\$temp
  5.         # ! later Remove-Item -Path C:\$temp -Recurse -Force
  6.     }

Can you help with this?

jvierra
Posts: 14867
Last visit: Fri Jul 23, 2021 7:52 pm
Answers: 12
Has voted: 3 times
Been upvoted: 10 times

Re: Datagridview | Select DataBoundItem

Post by jvierra »

To filter just set the RowFilter property. It takes one line.

$datagidview1.DatSource.DefaultView.RowFilter = ' your filter clause'

That is all we need to do to set the filter.

User avatar
rubberduck
Posts: 62
Last visit: Tue Jul 06, 2021 5:13 am

Re: Datagridview | Select DataBoundItem

Post by rubberduck »

Okay, I get it now.
I've been working on this too complicated.
I assumed that I could save the state of the active filter to the datagrid and then switch between these different filters.
But I have now found a solution.
Thank you

Locked