Reputation: 1027
I'm working on a Windows form application in Visual Studio 2010 Express. My Home form has one dataset with 4 tables. One of these tables is called "Category" and I want to manage this table from a separate form called Categories.
I followed the steps outlined by @Harm van der Haas in (Shared DataSet Over Multiple Forms C#) but I can't get it work.
In my Home form I have the following code:
public partial class frmHome : Form
{
public DataSet _dsMain;
public frmHome(DataSet dsMain)
{
_dsMain = dsMain;
InitializeComponent();
}
And in my program.cs I have:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
DataSet DS = new DataSet();
Application.Run(new frmHome(DS));
}
}
And in my Category form I have the following code:
public partial class frmCategory : Form
{
public DataSet ds2;
public frmCategory(DataSet dsMain)
{
ds2 = dsMain;
InitializeComponent();
}
private void frmCategory_Load(object sender, EventArgs e)
{
dgvCategory.DataSource = ds2;
dgvCategory.DataMember = "Category";
dgvCategory.Refresh();
}
}
Here I launch the form Category:
private void btnStart_Click(object sender, EventArgs e)
{
Globals.startTime = DateTime.Now;
frmCategory frmC = new frmCategory(_dsMain);
frmC.ShowDialog();
//updateActiveTrans();
}
But when the Category form loads my datagridview on it dgvCategory
doesn't show Category tables headers
Upvotes: 0
Views: 4425
Reputation: 321
Here is my simple way on how to pass Dataset
from one form to another. I'm using Visual Studio 2012 C# WPF with PostgreSQL as Database.
//ON MY FIRST FORM
//this is my connection - add it anywhere you want
//or change it according to your need
NpgsqlConnection iConnect = new NpgsqlConnection("Server = " + myModule.Server + ";Port = " + myModule.Port + ";User ID = " + myModule.UserID + ";Password = " + myModule.Password + ";Database = " + myModule.Database);
iConnect.Open();
NpgsqlCommand iQuery = new NpgsqlCommand("Select * from Table1");
iQuery.Connection = iConnect;
NpgsqlDataAdapter iAdapter = new NpgsqlDataAdapter(iQuery);
DataSet iDataSet = new DataSet();
iAdapter.Fill(iDataSet, "SET");
myModule.dtSet = iDataSet;//pass the dataset to myModule.cs
Then, click Project>>Add Class>>and rename it to myModule.cs
Then add the following code inside class myModule
//This is the code for the DATASET shared
static DataSet strDataset;
//this is the catcher of DATASET info request...
public static DataSet dtSet
{
get { return strDataset; }
set { strDataset = value; }
}
Now, this is how you will use the passed dataset on your second form.
//MY SECOND FORM
public Form2()
{
InitializeComponent();
DataSet iDataSet = new DataSet();//set the instance of Dataset
iDataSet = myModule.dtSet; //get the data from myModule and pass it to iDataSet
//now, you can use it here!
}
That's it and happy coding! ^_^
Upvotes: 1
Reputation: 1027
Ok, I figured it out. I was passing the DataSet incorrectly from my home form. This is the code that ended up working for me.
In my home(main) form I created the constructor like so:
public partial class frmHome : Form
{
public frmHome(DataSet dsMain)
{
InitializeComponent();
}
In my Program.cs I did the following:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
DataSet DS = new DataSet();
Application.Run(new frmHome(DS));
And this is how I open my Category form. --the form where I need to remotely view and manage a table from my dsMain DataSet--
frmCategory frmC = new frmCategory(dsMain);
frmC.ShowDialog();
And in my Category form I passed my dsMain declared in my home form, I declared a new public DataSet ds2 and made it equal to dsMain.
public partial class frmCategory : Form
{
public DataSet ds2;
public frmCategory(DataSet dsMain)
{
ds2 = dsMain;
InitializeComponent();
}
And finally in my form load event I bound ds2 to my dataGridView like so:
private void frmCategory_Load(object sender, EventArgs e)
{
dgvCategory.DataSource = ds2;
dgvCategory.DataMember = "Category";
dgvCategory.Refresh();
And it works like a charm now. I can view, edit and save my main DataSet from my Category form now. Thanks for the help @CharlesMighty, it helped a lot.
Upvotes: 0
Reputation: 572
in you frmCategory_Load event when assigning the grid a datasource you need to specify the table that you need. Here is the sample code:
public partial class frmCategory : Form
{
public DataSet ds2;
public frmCategory(DataSet dsMain)
{
ds2 = dsMain;
InitializeComponent();
}
private void frmCategory_Load(object sender, EventArgs e)
{
dgvCategory.DataSource = ds2.Tables[0]; //you can use .Tables[1] or the desired table
dgvCategory.DataMember = "Category";
dgvCategory.Refresh();
}
}
Upvotes: 1