Datasets

  • Thread starter Thread starter Cdude
  • Start date Start date
C

Cdude

What is the easiest way to make a dataset and all the data inside
viewable from multiple forms. The problem i experience is when i add
data in one form i cannot view that data i added to the dataset in
another form. Please help. Thanks
 
make it public static. (nobody yell at me, s/he asked for the easiest way)

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $19.99
 
I replied (twice in fact, with example) on this topic yesterday; it
looks like nntp swallowed them...

But as Alvin hints - you need to use the *same* DataSet (or other
model) instance in both forms for changes to be immeditely visible -
otherwise you'll need to do a lot of manual juggling. This could be by
passing a DataSet into the form, rather than creating one per form.

Some hasty re-typing... (below)

Marc

using System;
using System.Data;
using System.Windows.Forms;

static class Program
{
static void Main()
{
DataSet data = new DataSet("MyDataSet");
DataTable table = data.Tables.Add("MyDataTable");
table.Columns.Add("Name", typeof(string));
table.Columns.Add("DateOfBirth", typeof(DateTime));
table.Rows.Add("Fred", DateTime.Today);
table.Rows.Add("Jo", DateTime.Today.AddDays(-30));

Application.EnableVisualStyles();
MyForm form1 = new MyForm(),
form2 = new MyForm();
form1.Text = "Form 1";
form2.Text = "Form 2";
form1.DataMember = form2.DataMember = "MyDataTable";
form1.DataSource = form2.DataSource = data;
form1.Load += delegate { form2.Show(form1); };
Application.Run(form1);
}
}

class MyForm : Form
{
DataGridView grid;
public MyForm() {
InitializeComponent();
}
public string DataMember
{
get {return grid.DataMember;}
set {grid.DataMember = value;}
}
public object DataSource
{
get {return grid.DataSource;}
set {grid.DataSource = value;}
}

void InitializeComponent() {
grid = new DataGridView();
grid.Dock = DockStyle.Fill;
Controls.Add(grid);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top