DataGridView Selected Row data to Main Form

tdignan87

Well-known member
Joined
Jul 8, 2019
Messages
95
Programming Experience
Beginner
Hi I have two forms, the main form loads and there is a button on it. The user presses a button and it loads up a second form containing a datagridview. The user selects the row in the datagridview and I would like it to close automatically and populate a textbox in the main form with data they have selected from the dgv.

I'm familiar with passing data if the form that the data is passing too is closed first as of course the instance has not already been started. I've found youtube tutorials but they are mostly just passing datagridview data to a new form.

Any ideas, please? Thanks
Ive tried to do a constructor method but I don't know where I am going wrong.. argh.. something that seems so simple is frustrating..

Thanks
Tom

1572384767863.png
 
You neglected to ask a specific question. What is your question?

You should probably provide relevant code you've tried, and explain the problem with it.
 
Sorry
Question is

1. How do i put the selected cell in the datagridview to the other forms textbox.
Example , user clicks on datagridview row and i would like TESTING1 for example to appear In the commodity textbox in the other form
 
C#:
            //   string strValue = this.dataGridView1.CurrentRow.Cells[0].ToString();
            Home homeShow = new Home();
            homeShow.SupplierTxtBox.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();

I have this in the cell click event so it passes the current row cell value 0 to the Home form text box. Ofcourse the Home form is already open and is behind the form therefore its not working (and I understand why it's not as it starts a new instance, however i am stuck with how i can do this :)
 
The main problem is that you are going about this this the wrong way:
The user presses a button and it loads up a second form containing a datagridview. The user selects the row in the datagridview and I would like it to close automatically and populate a textbox in the main form with data they have selected from the dgv.
You are breaking encapsulation and the Law of Demeter by having the second form update the main form. You want to have the main form pull the value from the second form.

Assuming you have the following:
C#:
class MainForm : Form
{
    :
    Textbox _txtFieldToBeFilled;
    :
    void btnOpenSecondForm_Click(...)
    {
        :
    }
}

class SecondForm : Form
{
    :
    DataGridView _dgvUserChoices;
    :
    public string ValueToBePulled { get; private set; }
    :
    void btnOk_Click(...)
    {
    }
}

Inside MainForm.btnOpenSecondForm_Click() you would have something like:
C#:
var secondForm = new SecondForm(dataThatSecondFormNeeds);
if (secondForm.ShowDialog() == DialogResult.OK)
    _txtFieldToBeField.Text = secondForm.ValueToBePulled;
and your SecondForm.btnOk_Click() wolud have something like:
C#:
ValueToBePulled = _dgvUserChoices.CurrentRow.Cells[0].Value.ToString();
DialogResult = DialogResult.OK;

I need to look back now, but I believe that you previously told us that you were learning C# and WinForms by just going through random YouTube videos and blogposts. I really really recommend that instead of learning in a shotgun approach that you take time to do a more structured study. Find a book with good reviews and go through it in from front to end instead of cherry picking topics.
 
Very likely. I'm terrible with names...
 
Back
Top Bottom