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.
This topic is 5 years and 11 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
GHIsolierung
Posts: 62
Last visit: Tue Nov 02, 2021 12:46 am

Datagridview | Select DataBoundItem

Post by GHIsolierung »

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 2854 times
Excel Export.jpg
Excel Export.jpg (86.35 KiB) Viewed 2854 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 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
GHIsolierung
Posts: 62
Last visit: Tue Nov 02, 2021 12:46 am

Re: Datagridview | Select DataBoundItem

Post by GHIsolierung »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 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
GHIsolierung
Posts: 62
Last visit: Tue Nov 02, 2021 12:46 am

Re: Datagridview | Select DataBoundItem

Post by GHIsolierung »

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
This topic is 5 years and 11 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