dataGridView Doesnt get the datasource from another form?

raffs03

Member
Joined
Jan 18, 2012
Messages
6
Programming Experience
Beginner
I'm trying some little test on dataGridView
Form1
C#:
namespace dataTableTest
{
    public partial class Form1 : Form
    {
        //DataTable table = new DataTable();
        public Form1()
        {
            InitializeComponent();
            //table.Columns.Add("a");
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
           
            form2.ShowDialog();
           // MessageBox.Show("Add");
           // table.Rows.Add("AA");
           // dataGridView1.DataSource = table;
        }
    }
}
Form2
C#:
namespace dataTableTest
{
    public partial class Form2 : Form
    {
        DataTable table = new DataTable();
        public Form2()
        {
            InitializeComponent();
            table.Columns.Add("a");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 form1 = new Form1();
            table.Rows.Add("AA");
            form1.dataGridView1.DataSource = table;
            form1.dataGridView1.Refresh();
              MessageBox.Show("Add");
            this.Hide();
        }
    }
}

Why does the datagridview doesnt get the datasource from another form?
But when i try to uncomment the code on form1 and comment out the form2.ShowDialog();(this time form2 will not show and ignore.)
the dataGridView respond and add the data on it.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,219
Location
Sydney, Australia
Programming Experience
10+
Look at this code:
C#:
        private void button1_Click(object sender, EventArgs e)
        {
            [B][U]Form1 form1 = new Form1();[/U][/B]
            table.Rows.Add("AA");
            form1.dataGridView1.DataSource = table;
            form1.dataGridView1.Refresh();
              MessageBox.Show("Add");
            this.Hide();
        }
You're creating a new Form1 object and making changes to it. Why would that affect the old Form1 object that is already displayed? Remember that C# is an object-oriented language. That means that each form is an object. If you create an object in real life and change it, would you expect that to affect some other object of the same type, e.g. if you buy a house and paint it, does that mean that some other house is magically painted? If you want to affect the existing object then you must refer to the existing object, not create a new object and refer to that.
 

raffs03

Member
Joined
Jan 18, 2012
Messages
6
Programming Experience
Beginner
Hey i try to put the form1 instantiation on top instead on the CLickevent(because it always instantiate new form1 everytime i click the button)
C#:
namespace dataTableTest
{
    public partial class Form2 : Form
    {
        public DataTable table = new DataTable();
        Form1 form1 = new Form1(); //form1 instantiate on top
        public Form2()
        {
            InitializeComponent();
            table.Columns.Add("a");
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
             table.Rows.Add("AA");
            form1.dataGridView1.DataSource = table;
            form1.dataGridView1.Refresh();
              MessageBox.Show("Add");
            this.Hide();
           
             
        }
    }
}
but still the problem exist.
im trying to make a solution now.
 

raffs03

Member
Joined
Jan 18, 2012
Messages
6
Programming Experience
Beginner
Hey after a 15 mins of struggle i found a solution. I open some of my recent c++ CLR project. try the forward reference method and it works!
here i share my solution
Form1
C#:
namespace dataTableTest
{
    public partial class Form1 : Form
    {
        Form2 form2;
        public Form1()
        {
            InitializeComponent();
            form2 = new Form2(this);
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            form2.ShowDialog();
        }

        private void dataGridView1_Enter(object sender, EventArgs e)
        {
            
            //dataGridView1.DataSource = form2.table; 
        }
    }
}
Form2
C#:
namespace dataTableTest
{
    public partial class Form2 : Form
    {
        public DataTable table = new DataTable();
        private Form1 form1;
        public Form2(Form1 form1)
        {
            InitializeComponent();
            table.Columns.Add("a");
            this.form1 = form1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
             table.Rows.Add("AA");
            form1.dataGridView1.DataSource = table;
            form1.dataGridView1.Refresh();
              MessageBox.Show("Add");
            this.Hide();
           
             
        }
    }
}
The Enter event in dataGridView is working the same even if you not use forward reference method but not stable though.
By the way thanks for the answer :D
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,219
Location
Sydney, Australia
Programming Experience
10+
What you have there now will overcome that specific issue but still your approach is all wrong. Form2 shouldn't even know that Form1 exists. What should be happening is Form2 simply exposes the DataTable via a property and then Form1 retrieves it and binds it to its own grid. You should follow the Blog link in my signature below and check out my three-part post on Data Among Forms. The third part is where it explains how to do it properly but you should read the first two parts as well.
 
Top Bottom