Datagridview datasource

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 4 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
derhoeppi
Posts: 34
Last visit: Mon Nov 28, 2022 8:48 am

Datagridview datasource

Post by derhoeppi »

Hi,
i want to create a GUI with textboxes, buttons and a datagridview. The content of the textboxes should be written to the datagridview. I think the datagridview supports arraylists as datasource. How i could write to an arraylist and update the datagridview? With another button i want to delete some entries from the arraylist.
Can someone tell me a hint so i can solve my task?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagridview datasource

Post by jvierra »

You are asking for a complete solution. Forums are not good places to get solutions. They are more attuned to answering specific questions.

The best place to start would be to read some of the articles on GUI scripting in the Info center. They will help you to learn about how to use controls and how to update controls.

Here: https://info.sapien.com/index.php/guis/gui-scripting
And here: https://info.sapien.com/index.php/guis/gui-design-best-practice

To update a databound grid you would create a data table row object and add it to the grids data table. You can easily do this with a data table but not with an arraylist.

Attached is an example that adds rows to a table bound grid:
Attachments
Demo-DGVDataTable.psf
(37.52 KiB) Downloaded 542 times
User avatar
derhoeppi
Posts: 34
Last visit: Mon Nov 28, 2022 8:48 am

Re: Datagridview datasource

Post by derhoeppi »

Hi jvierra,

thanks for this example. I will test it with a datatable. Does the datagridview automatical updated when the datatable get or lost a row?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagridview datasource

Post by jvierra »

The code is all you need to see how this works. Just run the form and see how it works.
User avatar
derhoeppi
Posts: 34
Last visit: Mon Nov 28, 2022 8:48 am

Re: Datagridview datasource

Post by derhoeppi »

Hi jvierra,

is it possible to create some "hidden" columns in a datatable, so the datagridview doesn't show all columns? My second question belongs to edit the datagridview rows. In my project is set the datagridview in read only mode. A button reads out the selected row from the datagridview and put the value to textboxes. Now i want to update the selected row with the edited values from the textboxes. Is that possible or should i delete the full row and add the values from the textboxes to a new row in my datatable?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagridview datasource

Post by jvierra »

You can use a select statement to choose the rows or you can set any column as hidden.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagridview datasource

Post by jvierra »

derhoeppi wrote: Sun Nov 11, 2018 3:32 am Hi jvierra,

A button reads out the selected row from the datagridview and put the value to textboxes. Now i want to update the selected row with the edited values from the textboxes. Is that possible or should i delete the full row and add the values from the textboxes to a new row in my datatable?
You can just assign the new values to the attached DataTable row or to the row's "DataBoundItem".
User avatar
derhoeppi
Posts: 34
Last visit: Mon Nov 28, 2022 8:48 am

Re: Datagridview datasource

Post by derhoeppi »

Hi,

i tried to use the select method to update a value. But it says, that the value of the textbox is not a column name. Here is what i'm doing:

Code: Select all

#Initialize datatable
$dtProjectUser = New-Object System.Data.DataTable
$dtProjectUser.Columns.Add('Given Name', [string])
$dtProjectUser.Columns.Add('Surname', [string])
$dtProjectUser.Columns.Add('Login', [string])
$dtProjectUser.Columns.Add('UserRole', [string])

#The datatable get it rows through some textboxes

$UD_UserLogin = $textboxUserLogin.Text
$UD_UserPermissionChange = $dtProjectUser.Select("Login = $UD_UserLogin")
$UD_UserPermissionChange[0].UserRole = $comboboxUserRole.SelectedItem


If also try to use a where methode - it also failed.

Code: Select all

$UD_UserEdit = $dtProjectUser.where({ $_.Login -eq $UD_UserLogin })
$UD_UserEdit.UserRole = $comboboxUserRole.SelectedItem


If i want to see the output of $UD_UserEdit i only received "System.Data.DataRow". Why i dont see the columns of these row?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagridview datasource

Post by jvierra »

Select and where do not update anything.

A DataGridView is not a DataTable.

To update a DataTable row bound to a grid you need the row from the grid and its underlying "DataBoundItem" which is the table row,

Here are some examples of ways to use data binding with controls.

https://tech-comments.blogspot.com/2017/03/powershell-understanding-windows-forms.html

https://tech-comments.blogspot.com/2017/04/powershellunderstanding-windows-forms.html

https://tech-comments.blogspot.com/2017/04/binding-sqlserver-data-to-winforms-with.html
User avatar
derhoeppi
Posts: 34
Last visit: Mon Nov 28, 2022 8:48 am

Re: Datagridview datasource

Post by derhoeppi »

Hi,

i don't no what you mean. I have a datatable as datasource of the datagridview. When the project starts is it emtpy. I have several textboxes /comboboxes which values would be add new rows. If i add a new row to the datatable i can see that the datagridview update the new entry. Now i want to edit an entry of the datatable. Now you said i should create an databound between the datagridview and the textboxes / comboboxes?
This topic is 5 years and 4 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