Hi,
1. I have defind one variable with some value to one window form and I want to access this variable to variable in another window form.
2. I have defined one dataset (bound with some database) into one function defined into one form. I want this dataset should be accessible with bound data from any point of the application (may be another form).
Please assist me.
Thank you

accessing variable of one window form to another
akula
Öz.Net
First of all, the first form should know the instance of the form that contains the dataset, so when creating the form, you put it somewhere accessible in the first form.
namespace WindowsApplication1
{
public partial class Form1 : Form
{
Form2 _OtherForm;
public Form1()
{
InitializeComponent();
_OtherForm = new Form2();
_OtherForm.Show();
}
}
}
Then, in the Designer set the dataset designer property "Modifiers" on the other form as Public.
Now you'll be able to access the dataset from your first form, in my example trough _OtherForm.whateverthenameofthedatasetis
CharlesL
mcl2
ALi YUKSEL
Hi Kaetemi,
Thanks for your support.
Please be clear, I am not getting eaxctly.
Thank you
badandybad
{
Form2 _OtherForm;
public Form1()
{
InitializeComponent();
_OtherForm = new Form2();
_OtherForm.Show();
DataSet _PublicDataset = _OtherForm.dataSet1;
DataSet _PublicStaticDataset = Form2.dataSet2;
}
}
dreamlordzwolf - Michael Corpuz
Hi,
Thanks.
If I define dataset as following into one from "Form1" as:
public static DataSet dsName = new DataSet();
and then bind this dataset with some table from database.
Then we cant access this dataset into another form say "From2".
Is this not the perfect method
Thank you
Vander
I agree with WreckinCrew.
Here is how I do it typically. Create a public static class called Global.cs or something. I typically have a file like this in a DataLayer project to keep things seperate but how you want to organize your project is up to you. In this Global file do something like this:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace WindowsApplication3
{
public static class Global
{
private static CustomerDataSet _customers = null;
public static CustomerDataSet Customers
{
get
{
if (_customers == null)
{
CustomerDataSetTableAdapters.CustomerTableAdapter ta = new WindowsApplication3.CustomerDataSetTableAdapters.CustomerTableAdapter();
CustomerDataSet.CustomerDataTable dt = ta.GetData();
_customers = new CustomerDataSet();
_customers.Customer.Merge(dt);
}
return _customers;
}
}
}
}
There are two variables, one private and one public. One is used to check if data has already been loaded this way you will not be querying the database each time you access the dataset. You can change the logic to suit your needs.
In the Form_Load of whichever form you want to show the data on it is pretty simple then assuming you have a DataSet object, DataGridView, and a BindingSource all setup prior. Here is the Form_Load:
private void Form1_Load(object sender, EventArgs e)
{
this.customerDataSet.Merge(Global.Customers);
}
I'm a firm believer in seperating data, business logic, and things out. As I tell people all the time, what happens if someone says, hey this app you built is great, can you build us a Mobile version of it now You'll have a much easier time doing so if you seperate things initially since your application is simply an interface and everything else is seperate. Just my $.02. Hope this helps.
-The Elder
ipointer
Hi,
I am not asking for Window Controls. My concern is with Dataset defined into .cs file of Form1. There is not any other way instead of static.
Thank you
salimk786
And if you use static, you just do Form2.datasetname. You don't get the static things when typing forminstancename.