I have a program where am trying to access a dataset in the main form from a 2nd Winfrom. I have created a form 1 ref but when I use this to access the datatable in the dataset it doesnt seem to work. The following code is for Form 2 and I Movset1 is the dataset in the main form. I then try to use a dataview to access the main dataset.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Common;

namespace MovieDiary
{
    public partial class Form2 : Form
    {


        DataView Dv2 = new DataView();
        public Form2()
        {

            InitializeComponent();

        }

        public Form1 F1 = new Form1();


        public void Form2_Load(object sender, System.EventArgs e)
        {

        }







        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show(" Please enter a title to be deleted ");
                return;


            }


            Dv2 = new DataView(F1.Movset1.Tables[0]);
            F1.dataGrid1.DataSource = Dv2;
            Dv2.RowFilter = "Title ='" + textBox1.Text + "'";





        }
    }
}

One option would be to set accessor modifier of the dataSet to public static. Then you can access to dataSet simply ba Form name, like:

class MyClass
{
    public static DataSet myDataSet;
}

class MyOtherClass
{
    void MyMethod()
    {
        DataSet ds2 = MyClass.myDataSet;
    }
}

Next option would be (not to use a static access modifer), to use a class`s reference and access to dataSet by it:

    class MyClass
    {
        public DataSet myDataSet; //must be public to access from other classes
        void OpenOtherClass()
        {
            MyOtherClass c2 = new MyOtherClass(this); //pass a reference of this class

        }
    }

    class MyOtherClass
    {
        MyClass mc;
        public MyOtherClass(MyClass _mc)
        {
            this.mc = _mc;
        }
        void MyMethod()
        {
            DataSet ds2 = mc.myDataSet;
        }
    }

Or even 3rd option would be to use events. And trugger it to access to it. But this is not what you would like to learn yet. Maybe later.

Hope it helps,
bye

Thanks for the reply. I have tried your first solution and this seems to have worked thanks very much. I have made the dataset public static and created a method in the 2nd form and passed a reference from the main dataset to another dataset in the method. Thanks again for your help

Anytime.
But remember, marking members as static, this means they stay in the memory as long as an application is alive.
Personally I would avoid using static members, and rather choose 2nd option I gave you.
Just for you notice.

ps: if issue has been resalved, please mark the thread as answered.
thx
bye

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.