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
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 »

No. I posted some examples of ho to do databinding and how to add a row or edit a row using the underlying table. Until you understand how that works your posts will be hard for me to understand because you are just posting guesses without any understanding.

I also have no idea if your code is correctly designed. There is no need to create a new row object with a DataTable as it has a method that generates a row when needed.

$r = $dataGridView.DataSource.NewRow()

or
$dataGridView.DataSource.Rows.Add(<array 0f values>)
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 following is one way to get the selected rows data table row to update it.

Code: Select all

$datagridview1_CellDoubleClick=[System.Windows.Forms.DataGridViewCellEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
	$datagridview1.Rows[$_.RowIndex].DataBoundItem
}
User avatar
derhoeppi
Posts: 34
Last visit: Mon Nov 28, 2022 8:48 am

Re: Datagridview datasource

Post by derhoeppi »

Hi,
maybe it's difficult to understand what i mean but i created a little part of my project. If i click on the edit button then i get the values of the selected row in the datagridview. No i want to Change the permission role and write them back to the datagridview / datatable as source. Later i want to change something else.
Attachments
DatagridviewExampleDatatable.psf
(31.32 KiB) Downloaded 132 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 datasource

Post by jvierra »

I dug around and found this old example of how to add a row to a DGV that is populated from a set of textboxes.
Attachments
Demo-DGVDataTable.psf
(37.52 KiB) Downloaded 121 times
User avatar
derhoeppi
Posts: 34
Last visit: Mon Nov 28, 2022 8:48 am

Re: Datagridview datasource

Post by derhoeppi »

Hi,

this file is internal linked to a New-PersonForm.ps1. After them it will create a new row but i want to update an existing 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 »

Sorry I forgot to upload the form. Here it is.
Attachments
New-PersonForm.ps1
(5.89 KiB) Downloaded 119 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 datasource

Post by jvierra »

I quickly modified the example to also allow editing. Double click any row to open in edit form.

I also embedded the subform for now. It should be a project with two PSF files but I wanted to make it simple demo when I first built it about two years ago. Maybe I will clean it up as a project later.

The code also needs validation and error handling to be a robust editor. It would also work with less code if I used full data binding.
Attachments
Demo-DGVDataTable3.psf
(37.69 KiB) Downloaded 126 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 datasource

Post by jvierra »

In the above example note that you double click a row to edit.
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 »

This version adds the ability to cancel both an edit and an add.
Attachments
Demo-DGVDataTable3.psf
(37.69 KiB) Downloaded 100 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 datasource

Post by jvierra »

This one adds the "delete" ability using the "Delete" key. It prompts for confirmation.

Note that these changes all take only a few lines of code. Deleting is three lines.

Point is that once you understand how forms are intended to work and are programmed nothing should take more than a few lines.
Attachments
Demo-DGVDataTable3.psf
(38.01 KiB) Downloaded 152 times
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