Pass DataGridView selected cell value to TextBox in another Form

tdignan87

Well-known member
Joined
Jul 8, 2019
Messages
95
Programming Experience
Beginner
Hi
I have the following code; although its not passing the value. Can anyone give me some guidance please?
AdjustCodeTxtBox is returning with no data.
C#:
 ISForm myForm = new ISForm();

              

              

                 ProductDGV.CurrentRow.Cells[0].Value = myForm.AdjustCodeTxtBox.Text;
                this.Close();
 
I assume there is a reason you are creating a new instance of ISForm(). Are you calling ShowDialog() or Show() on that instance at some point to actually display that form instance and have it interact with the user?
 
Hi Mate.
Yeah ISform is the main form; they click a button that opens another form containing the datagridview. the user puts in a filter; selects the row and that data needs to go back to the main form and populate the textboxes.
ShowDialog is used to open the form containing the code I posted.
 
Remember that assignment operator works from right to left. The left hand side is the destination. The right hand side is the source. This like of code:
C#:
ProductDGV.CurrentRow.Cells[0].Value = myForm.AdjustCodeTxtBox.Text;
is taking the value from the text box and assigning it to a cell in the data grid view. Based on your title and your last post, it sounds like you want the opposite: go from data grid view cell to the text box. That means the sides need to swapped.
 
Hi Skydiver
No sorry its taking the value from the dgv selected row, and placing it in the textbox.
So basically
1. op opens form
2. Op clicks button that opens the datagridview with search filter. Op filters out and selects the cell in the DGV
3. DGV closes and puts the selected cell value into the textbox.

Thanks
 
Yeah i changed the code to
C#:
myForm.AdjustCodeTxtBox.Text = this.ProductDGV.CurrentRow.Cells[1].Value.ToString();
                this.Close();

Still returns nothing to the textbox.
 
Here's one I created earlier. 11 years earlier.

 
Back
Top Bottom