Ok I'm going to walk you through a possible (note - possible and not necessarily the best) solution because I don't like the one that has already been posted.
A couple of things you have to consider ... what is displaying the second form and how do you keep a note of it's instance.
I've created a simple WinForm application that has two forms - Form1 and Form2.
Form1
has a DataGridView (
dataGridView1
) which for the purposes of this has a single column populated with numbers 0 to 99.
Form2
has a single TextBox (
textBox2
)
I'm using the
SelectionChanged
event on the DataGridView to handle the display in the second form.
Here's my code - I've tried to add appropriate comments to explain my thinking.
using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
private Form2 f2 = new Form2();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 100; i++)
dataGridView1.Rows.Add(i.ToString());
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (f2.Visible == false)
f2.Show();
TextBox t = (TextBox)f2.Controls.Find("textBox2", true)[0];
t.Text = dataGridView1.SelectedCells[0].Value.ToString();
}
}
}
If you want to research other ways (and you should) try starting here with this
code project article[
^]