Datagrid binding

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 4 years and 5 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
venki45609
Posts: 4
Last visit: Sun Oct 20, 2019 11:10 pm

Datagrid binding

Post by venki45609 »

Hi,

I am new to forms, While developing code in powershell studio I am facing some issues.

script is, multiform project, so when clicked on add servers in one form will open a 2nd form and in the textbox when I add servers and click ok, the form should close and bind this data to a datagridview in the first form as new lines for separate servers.

I am able to open a new form, but not able to bind the data to the datagrideview

can someone please help?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagrid binding

Post by jvierra »

Hi - welcome to forms. Unfortunately there is not enough information to tell us what it is that you are having an issue with.

I recommend starting by reading through some of the articles in the "Information Center" section of this site. You will find many discussions that will help you to understand Forms and how to use them in PowerShell.

https://info.sapien.com/index.php/guis

The following article will explain how to pass and return values between forms:

https://info.sapien.com/index.php/guis/ ... sing-forms

The article says that form names start with "Call-". That has been changed to "Show-"
venki45609
Posts: 4
Last visit: Sun Oct 20, 2019 11:10 pm

Re: Datagrid binding

Post by venki45609 »

I am able to copy the data to the Grid. But the data which is entered in Textbox1, is not copied to grid as rows.
instead it is copying in single cell.

attached the forms. Main form and add serves form.

Can someone please help?
Attachments
MainForm.psf
(29.44 KiB) Downloaded 88 times
AddServer.psf
(18.03 KiB) Downloaded 77 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagrid binding

Post by jvierra »

You cannot at plain text to a grid. You must add rows and satisfy all columns for each row.

See: https://docs.microsoft.com/en-us/dotnet ... tem_Object___



$row = @('col1','col2',col3', …)
$datagridview1.Rows.Add($row)
venki45609
Posts: 4
Last visit: Sun Oct 20, 2019 11:10 pm

Re: Datagrid binding

Post by venki45609 »

Thanks for the reply, still didnt get as I am new to coding.

Can you please help me how to add the text from textbox1 to Grid as rows?

can you share the snippet in the code?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagrid binding

Post by jvierra »

I posted the answer in my previous post. You must add the textbox as a column in an array of column text values. Try the code example I posted after reading the link I posted.
venki45609
Posts: 4
Last visit: Sun Oct 20, 2019 11:10 pm

Re: Datagrid binding

Post by venki45609 »

I removed all columns except ServerName, still the data comes to only one column cell
  1. function Add-Servers
  2. {  
  3.     if ((Show-AddServer_psf) -eq 'OK')
  4.     {
  5.         #When you call a form, you get return value variables:
  6.        
  7.         $Servers = @($AddServer_addservers_textbox1)
  8.         foreach ($server in $Servers)
  9.         {
  10.             $datagridview1.Rows.Add($server)
  11.         }
  12.        
  13.     }
  14. }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagrid binding

Post by jvierra »

One textbox - one column. That is how it works. Why not just type the values directly into the grid columns? That is how the grid is designed to work.
This topic is 4 years and 5 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