Page 1 of 1

Display ChildForm DataGridView in ParentForm

Posted: Fri Jul 30, 2021 8:27 pm
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 }

Re: Display ChildForm DataGridView in ParentForm

Posted: Fri Jul 30, 2021 9:37 pm
by jvierra
You cannit display an object collection in a textbox it must be converted into text first.

Re: Display ChildForm DataGridView in ParentForm

Posted: Sat Jul 31, 2021 8:45 am
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 4460 times

Re: Display ChildForm DataGridView in ParentForm

Posted: Sat Jul 31, 2021 8:54 am
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.

Re: Display ChildForm DataGridView in ParentForm

Posted: Sat Jul 31, 2021 11:25 am
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 4447 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 4447 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 4447 times

Re: Display ChildForm DataGridView in ParentForm

Posted: Sun Aug 01, 2021 3:12 am
by jvierra
You have to reference teh textbox and not the gridview.

Re: Display ChildForm DataGridView in ParentForm

Posted: Tue Aug 03, 2021 5:01 am
by harringg
That was the solution for this use case. Thank you.