You don't have to sync the form with the current row. That's what a BindingSource
is for.
When you do simple binding to a BindingSource
, then every time its current item changes, the bound controls get updated, and every time the values in the bound controls change, the underlying values in the bound item get updated. When you do complex binding (i.e. the bound control displays the BindingSource
's list, not just the current item), changing Position
on the BindingSource
will also change the current position in the bound control, and vice versa. So in this case, you want to bind the controls on your second form using simple binding, and the DataGridView
on your first using complex binding.
The only unusual thing you need to do is make sure that both forms are using the same BindingSource
. When you do that, clicking on a new row in the DataGridView
updates the Position
on the BindingSource
, and the BindingSource
pushes the values from the current bound item out to all of the simply-bound controls that are bound to it.
This is easily accomplished. Assuming that Form1
is the form with the DataGridView
, and Form2
is the one with the simply-bound controls, do this:
In Form1:
private BindingSource Source = new BindingSource();
Form1_Load(object sender, EventArgs e)
{
// set Source's DataSource to your DataTable here.
mainDataGridView.DataSource = source;
// create DataGridView columns and their bindings here.
Form2 f2 = new Form2();
f2.TopMost = true;
f2.Source = Source;
f2.Show();
}
In Form2:
public BindingSource Source { get; set; }
public void Form2_Load(object sender, EventArgs e)
{
idTextBox.DataBindings.Add("Text", Source, "id");
descriptionTextBox.DataBindings.Add("Text", Source, "description")
}