Display ChildForm DataGridView in ParentForm

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.
Locked
User avatar
harringg
Posts: 18
Last visit: Wed Sep 08, 2021 11:37 am

Display ChildForm DataGridView in ParentForm

Post by harringg »

I have a Parent and Child Form.
Parent used to display results from Child.

Child textbox can display first cell of selected row in richtextbox1 I have dozens of Parent/Child forms passing data successfully. Just not getting desired datagridview data to display

Code: Select all

$datagridviewResults_CellMouseClick=[System.Windows.Forms.DataGridViewCellMouseEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellMouseEventArgs]
	#TODO: Place custom script here
	$richtextbox1.Text = $datagridviewResults.SelectedRows[0].Cells[0].Value.ToString()
}
I'm trying to pass that value to the Parent textbox ($richtextboxDisplayMessageMainForm)

Code: Select all

$buttonShowCfGridDataGridVi_Click={
	#Note: ChildForm Button property:'DialogResult' must be set to 'OK'
	if ((Show-cfGrid-DataGridView_psf) -eq 'OK')
	{
		$richtextboxDisplayMessageMainForm.Text = $cfGrid_DataGridView_datagridviewResults.SelectedRows[0].Cells[0].Value.ToString()
	}
}
It's reading the values, because when I run

Code: Select all

$buttonShowCfGridDataGridVi_Click={
	#Note: ChildForm Button property:'DialogResult' must be set to 'OK'
	if ((Show-cfGrid-DataGridView_psf) -eq 'OK')
	{
		$richtextboxDisplayMessageMainForm.Text = $cfGrid_DataGridView_datagridviewResults
	}
}
$richtextboxDisplayMessageMainForm displays:

DataGridViewTextBoxCell { ColumnIndex=0, RowIndex=4 } DataGridViewTextBoxCell { ColumnIndex=1, RowIndex=4 } DataGridViewTextBoxCell { ColumnIndex=2, RowIndex=4 } DataGridViewTextBoxCell { ColumnIndex=3, RowIndex=4 } DataGridViewTextBoxCell { ColumnIndex=4, RowIndex=4 } DataGridViewTextBoxCell { ColumnIndex=5, RowIndex=4 } DataGridViewTextBoxCell { ColumnIndex=6, RowIndex=4 } DataGridViewTextBoxCell { ColumnIndex=7, RowIndex=4 } DataGridViewTextBoxCell { ColumnIndex=8, RowIndex=4 } DataGridViewTextBoxCell { ColumnIndex=9, RowIndex=4 }

jvierra
Posts: 14924
Last visit: Wed Sep 15, 2021 8:13 am
Answers: 14
Has voted: 3 times
Been upvoted: 11 times

Re: Display ChildForm DataGridView in ParentForm

Post by jvierra »

You cannit display an object collection in a textbox it must be converted into text first.

User avatar
harringg
Posts: 18
Last visit: Wed Sep 08, 2021 11:37 am

Re: Display ChildForm DataGridView in ParentForm

Post by harringg »

1. Launch ChildForm from Parent Form
2. Load Grid content in ChildForm
3. Select row in dataGridView (is shown in richtextbox1 on ChildForm)
4. Click buttonExit (designated as DialogResult = 'OK')
5. Desired result is to view 'smss.exe' in richtextboxDisplayMessageMainForm on ParentForm (after closing ChildForm).

Step 3 Code:

Code: Select all

$datagridviewResults_CellMouseClick=[System.Windows.Forms.DataGridViewCellMouseEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellMouseEventArgs]
	#TODO: Place custom script here
	$richtextbox1.Text = $datagridviewResults.SelectedRows[0].Cells[0].Value.ToString()
}
Step 1 Code:

Code: Select all

$buttonShowCfGridDataGridVi_Click={
	#Note: ChildForm Button property:'DialogResult' must be set to 'OK'
	if ((Show-cfGrid-DataGridView_psf) -eq 'OK')
	{
		$richtextboxDisplayMessageMainForm.Text = $cfGrid_DataGridView_datagridviewResults.SelectedRows[0].Cells[0].Value.ToString()
	}
}
ERROR: Cannot index into a null array

It appears the issue I'm trying to overcome is how the transfer of content from ChildForm to ParentForm using DataGridView.

Is there a code change syntax in "Step 1 Code" I can improve to get the result I'm looking for?
2021-07-31_10-26-09.png
2021-07-31_10-26-09.png (38.04 KiB) Viewed 2518 times

User avatar
Alexander Riedel
Posts: 7764
Last visit: Thu Sep 16, 2021 11:51 pm
Answers: 6
Been upvoted: 11 times

Re: Display ChildForm DataGridView in ParentForm

Post by Alexander Riedel »

When you exit a form, the object no longer exists. So before you exit, you need to transfer any selection you need to a script or global scope variable of a matching type.
So when in the Exit button handler of your child form
$global:ChildFormReturn = "This is the text I want to return"

And in the parent form, you can then use that variable to populate a result field.
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
harringg
Posts: 18
Last visit: Wed Sep 08, 2021 11:37 am

Re: Display ChildForm DataGridView in ParentForm

Post by harringg »

Same ParentForm (part of a MultiForm Project: BuildingBlocks).

I'm using the tutorial from June Blender and Sapien as a foundation. https://www.youtube.com/watch?v=SdaLaGReNk0

My current project is to take the various types of Form Controls, set them up as an ChildForm and return the results back to the ParentForm. That will give me a resource of code to use in more complex projects.
2021-07-31_13-23-11.png
2021-07-31_13-23-11.png (11.75 KiB) Viewed 2505 times
DatePicker ChildForm

1. Launch ChildForm from Parent Form
2. Pick Date from DatePicker in ChildForm
3. Result is shown in richtextbox1 on ChildForm
4. Click buttonOK (designated as DialogResult = 'OK')
5. Result is passed to richtextboxDisplayMessageMainForm on ParentForm (after closing ChildForm).

Step 3 Code:

Code: Select all

$datetimepicker1_ValueChanged={
	#Note: Updates $richtextbox1.text when $dateTimePicker1 is changed (ie selection is made)
	$buttonOK.Enabled = $false
	if ($dateTimePicker1.Value.ToString() -ne $null)
	{
		#Note: Renables OK Button (makes it 'clickable')
		$buttonOK.Enabled = $true
		#Note: Puts ShortDateString in RichTextBox
		$richtextbox1.Text = $dateTimePicker1.Value.ToShortDateString()
	}	
}
Step 1 Code:

Code: Select all

$buttonShowcfDateTimePicker_Click={
	#Note: ChildForm Button property:'DialogResult' must be set to 'OK'
	if ((Show-cfDateTimePicker_psf) -eq 'OK')
	{
		$richtextboxDisplayMessageMainForm.Text = $cfDateTimePicker_dateTimePicker1.ToShortDateString()
	}
}
There is no extra event handler in ChildForm DateTimePicker. I thought the $cfDateTimePicker variable "handled" all the transfer of content from child to parent form. In my OP, the data is "transferred", I just can't figure out the correct code to display it as the same "format" in the ChildForm.
2021-07-31_12-51-59.png
2021-07-31_12-51-59.png (52.87 KiB) Viewed 2505 times
Data from ChildFormDataGridView is "transferred" (in memory, in scope, ect...) to Parent Form. My ask is how to get it return the same data as is seen in the Child Text box, which in this example is the text string: 'smss.exe'
2021-07-31_13-17-42.png
2021-07-31_13-17-42.png (12.62 KiB) Viewed 2505 times

jvierra
Posts: 14924
Last visit: Wed Sep 15, 2021 8:13 am
Answers: 14
Has voted: 3 times
Been upvoted: 11 times

Re: Display ChildForm DataGridView in ParentForm

Post by jvierra »

You have to reference teh textbox and not the gridview.

User avatar
harringg
Posts: 18
Last visit: Wed Sep 08, 2021 11:37 am

Re: Display ChildForm DataGridView in ParentForm

Post by harringg »

That was the solution for this use case. Thank you.

Locked