1

I am using Windows Forms on .NET 4.5.2. I have 2 Forms. Form1 has a DataGridView that contains field from database and a button that shows Form2 when clicked. Form2 has a TextBox. I want to fill it with text from one of Form1 DataGridView fields when I click a button in Form1. Is it possible? The Form2 Textbox modifier is set to public but it is still not working.

I have tried:

private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    Form2 fr = new Form2();
    int row = DataGridView1.CurrentRow.Index;
    fr.Textbox1.Text = Convert.ToString(DataGridView1[0, row].Value);
    fr.Textbox2.Text = Convert.ToString(DataGridView1[1, row].Value);    
}

private void button1_Click(object sender, EventArgs e)
{
    Form2 fr = new Form2();
    fr.ShowDialog();  
}
Cyb Fyr
  • 39
  • 1
  • 6

3 Answers3

0

First you should do the Form1's datagridview modifiers public. When you click the button from Form1, open the Form2 and write this code to Form2_Load().

 Form1 frm = (Form1)Application.OpenForms["Form1"];
 int row = frm.DataGridView1.CurrentRow.Index;
 Textbox1.Text = Convert.ToString(frm.DataGridView1[0, row].Value);
 Textbox2.Text = Convert.ToString(frm.DataGridView1[1, row].Value);

This should work.

  • 1
    However this breaks encapsulation. Probably author doesn't care about it in this case, but still it is be better to be warned. – ymdred16 Nov 29 '19 at 06:21
  • You are right. Thanks for the warning. But like you said, the author probably does not care. – Bayram ESER Nov 29 '19 at 08:38
0

Your problem exists because you are using ShowDialog() method instead of Show(). From this answer on Stack Overflow:

The Show function shows the form in a non modal form. This means that you can click on the parent form.

ShowDialog shows the form modally, meaning you cannot go to the parent form.

Somehow that interacts with passing the value from one form to another, I think it happens because the first form gets blocked (paused) after ShowDialog() method therefore preventing you from copying the value from DataGridView.

If you used ShowDialog() method on purpose, then you can try to bypass this limitation somehow. For example, I managed to pass the required value from DataGridView to TextBox in a newly created Form (lets call it Form2) by using the Owner property (check this answer too) and Form.Shown event. You can try replacing your code with this piece in your button1_Click event handler (or probably just creating Shown event handler in your file with Form2 class):

Form2 fr = new Form2();
int row = DataGridView1.CurrentRow.Index;
fr.Shown += (senderfr, efr) => 
{
    // I did null check because I used the same form as Form2 :) 
    // You can probably omit this check.
    if (fr.Owner == null) return;

    var ownerForm = (Form1)fr.Owner;
    fr.Textbox1.Text = ownerForm.DataGridView1[0, row].Value.ToString();
    fr.Textbox2.Text = ownerForm.DataGridView1[1, row].Value.ToString();
};
fr.ShowDialog(this);  

P.S. Why would you use Convert.ToString() instead of simply calling ToString() method on Value property like I did in the example?

Community
  • 1
  • 1
ymdred16
  • 98
  • 3
  • 15
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/203389/discussion-on-answer-by-ymdred16-how-do-i-pass-datagridview-selected-row-value-t). – Baum mit Augen Nov 30 '19 at 16:25
0
  Form2 fr = new Form2();
  private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    int row = DataGridView1.CurrentRow.Index;
    fr.Textbox1.Text = Convert.ToString(DataGridView1[0, row].Value);
    fr.Textbox2.Text = Convert.ToString(DataGridView1[1, row].Value);    
}

private void button1_Click(object sender, EventArgs e)
{
    fr.ShowDialog();  
}
Umair Rasheed
  • 436
  • 4
  • 10
  • This will never work. How did you read other answers before posting yours one? – ymdred16 Nov 30 '19 at 12:30
  • according to your code, you make objects 2 times every time you make a new object in first part you assigned values to object then next you also make a new object which is not getting the values. – Umair Rasheed Nov 30 '19 at 12:38
  • the new instance of object will never get your values. – Umair Rasheed Nov 30 '19 at 12:39
  • First of all, that's not my code neither am I the one who asked the question. I misunderstood your code at first, probably this would work. But there are a few cons: you break encapsulation and what if it is needed to create a new form every time the button is clicked? – ymdred16 Nov 30 '19 at 12:54