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
jvierra
Posts: 13732
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Datagridview datasource

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

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

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

Re: Datagridview datasource

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

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

Re: Datagridview datasource

Post by derhoeppi » Sun Nov 11, 2018 11:41 am

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

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

Re: Datagridview datasource

Post by jvierra » Sun Nov 11, 2018 11:45 am

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

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

Re: Datagridview datasource

Post by derhoeppi » Sun Nov 11, 2018 11:53 am

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.

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

Re: Datagridview datasource

Post by jvierra » Sun Nov 11, 2018 12:09 pm

Sorry I forgot to upload the form. Here it is.
Attachments
New-PersonForm.ps1
(5.89 KiB) Downloaded 33 times

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

Re: Datagridview datasource

Post by jvierra » Sun Nov 11, 2018 12:30 pm

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

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

Re: Datagridview datasource

Post by jvierra » Sun Nov 11, 2018 6:28 pm

In the above example note that you double click a row to edit.

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

Re: Datagridview datasource

Post by jvierra » Sun Nov 11, 2018 6:33 pm

This version adds the ability to cancel both an edit and an add.
Attachments
Demo-DGVDataTable3.psf
(37.69 KiB) Downloaded 23 times

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

Re: Datagridview datasource

Post by jvierra » Sun Nov 11, 2018 6:53 pm

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

Locked