You could put the
DataTable
and
BindingSource
in a
Static class, then you can access them from all forms.
Program.cs
using System;
using System.Windows.Forms;
using System.ComponentModel;
namespace TestForm1
{
static class Program
{
public static BindingList<MyClass> masterBindingList;
[STAThread]
static void Main()
{
Application.Run(new Form10());
}
public class MyClass
{
public string Title { get; set; }
}
}
}
Form10.cs
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace TestForm1
{
public partial class Form10 : Form
{
public Form10()
{
InitializeComponent();
this.Init();
}
private void Init()
{
Program.masterBindingList = new BindingList<Program.MyClass>();
this.dataGridViewMaster.DataSource = Program.masterBindingList;
}
private void ButtonAddRowClick(object sender, EventArgs e)
{
var rowIndex = this.dataGridViewMaster.RowCount;
this.AddRow(rowIndex);
this.ScrollToRow(rowIndex);
}
private void AddRow(int rowIndex)
{
var myRow = new Program.MyClass { Title = "Row " + rowIndex.ToString() };
Program.masterBindingList.Add(myRow);
}
private void ScrollToRow(int rowIndex)
{
this.dataGridViewMaster.ClearSelection();
this.dataGridViewMaster.FirstDisplayedScrollingRowIndex = rowIndex;
this.dataGridViewMaster.Focus();
}
private void buttonShow_Click(object sender, EventArgs e)
{
var form10b = new Form10b();
form10b.ShowDialog();
}
}
}
Form10b.cs
using System.Windows.Forms;
namespace TestForm1
{
public partial class Form10b : Form
{
public Form10b()
{
InitializeComponent();
dataGridView1.DataSource = Program.masterBindingList;
}
}
}