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.
User avatar
derhoeppi
Posts: 34
Joined: Sun Apr 06, 2014 11:10 pm

Datagridview datasource

Post by derhoeppi » Fri Nov 09, 2018 10:12 am

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?

User avatar
jvierra
Posts: 13798
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Datagridview datasource

Post by jvierra » Fri Nov 09, 2018 10:30 am

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 147 times

User avatar
derhoeppi
Posts: 34
Joined: Sun Apr 06, 2014 11:10 pm

Re: Datagridview datasource

Post by derhoeppi » Fri Nov 09, 2018 11:17 am

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?

User avatar
jvierra
Posts: 13798
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Datagridview datasource

Post by jvierra » Fri Nov 09, 2018 11:29 am

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
Joined: Sun Apr 06, 2014 11:10 pm

Re: Datagridview datasource

Post by derhoeppi » Sun Nov 11, 2018 4:32 am

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?

User avatar
jvierra
Posts: 13798
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Datagridview datasource

Post by jvierra » Sun Nov 11, 2018 4:51 am

You can use a select statement to choose the rows or you can set any column as hidden.

User avatar
jvierra
Posts: 13798
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Datagridview datasource

Post by jvierra » Sun Nov 11, 2018 8:02 am

derhoeppi wrote:
Sun Nov 11, 2018 4: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
Joined: Sun Apr 06, 2014 11:10 pm

Re: Datagridview datasource

Post by derhoeppi » Sun Nov 11, 2018 8:43 am

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?

User avatar
jvierra
Posts: 13798
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Datagridview datasource

Post by jvierra » Sun Nov 11, 2018 9:03 am

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
Joined: Sun Apr 06, 2014 11:10 pm

Re: Datagridview datasource

Post by derhoeppi » Sun Nov 11, 2018 9:34 am

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?

Locked