Page 1 of 2

Datagridview datasource

Posted: Fri Nov 09, 2018 9:12 am
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?

Re: Datagridview datasource

Posted: Fri Nov 09, 2018 9:30 am
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:

Re: Datagridview datasource

Posted: Fri Nov 09, 2018 10:17 am
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?

Re: Datagridview datasource

Posted: Fri Nov 09, 2018 10:29 am
by jvierra
The code is all you need to see how this works. Just run the form and see how it works.

Re: Datagridview datasource

Posted: Sun Nov 11, 2018 3:32 am
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?

Re: Datagridview datasource

Posted: Sun Nov 11, 2018 3:51 am
by jvierra
You can use a select statement to choose the rows or you can set any column as hidden.

Re: Datagridview datasource

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

Re: Datagridview datasource

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

Re: Datagridview datasource

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

Re: Datagridview datasource

Posted: Sun Nov 11, 2018 8:34 am
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?