Page 2 of 2

Re: Datagridview datasource

Posted: Sun Nov 11, 2018 9:47 am
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>)

Re: Datagridview datasource

Posted: Sun Nov 11, 2018 9:52 am
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
}

Re: Datagridview datasource

Posted: Sun Nov 11, 2018 11:41 am
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.

Re: Datagridview datasource

Posted: Sun Nov 11, 2018 11:45 am
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.

Re: Datagridview datasource

Posted: Sun Nov 11, 2018 11:53 am
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.

Re: Datagridview datasource

Posted: Sun Nov 11, 2018 12:09 pm
by jvierra
Sorry I forgot to upload the form. Here it is.

Re: Datagridview datasource

Posted: Sun Nov 11, 2018 12:30 pm
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.

Re: Datagridview datasource

Posted: Sun Nov 11, 2018 6:28 pm
by jvierra
In the above example note that you double click a row to edit.

Re: Datagridview datasource

Posted: Sun Nov 11, 2018 6:33 pm
by jvierra
This version adds the ability to cancel both an edit and an add.

Re: Datagridview datasource

Posted: Sun Nov 11, 2018 6:53 pm
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.