datagrid access from another form

后端 未结 3 1528
梦如初夏
梦如初夏 2021-01-27 12:37

I have datagridview in form1. How can I access datagridview from form2.

  private void button1_Click(object sender, EventArgs e)
  {
          string sql1 = \"in         


        
相关标签:
3条回答
  • 2021-01-27 13:26

    You need a reference to the actual Form in order to access its members. (And those members will need to be public.)

    Just calling something like this:

    Form1.dataGridView
    

    won't work because Form1 is just a type, it's not a reference to an instantiated object in memory. That's how you'd reference static members, which isn't the case here. The DataGridView is an instance member. So you need a reference to the instance of Form1. Something more like:

    firstForm.dgv
    

    where firstForm is a variable on Form2 (or passed into the method as an argument from Form1, where the argument would just be this, etc.) and dgv is the public member on Form1 which represents the DataGridView.

    Something like this:

    public class Form1
    {
        public DataGridView DGV { get; set; }
    
        private void DoSomething()
        {
            var anotherForm = new Form2();
            anotherForm.DoSomethingElse(this);
        }
    }
    
    public class Form2
    {
        public void DoSomethingElse(Form1 firstForm)
        {
            var data = firstForm.DGV.DataSource;
        }
    }
    

    Note that I left out a lot of WinForms stuff here. That's intentional. This is just to demonstrate the concept at the code level. What the forms inherit from, how they're instantiated, how they're held in memory, that's all another concern.

    How you set this up is up to you. I'm not well versed in WinForms development, but I imagine there are better ways to accomplish this. In order to determine that, though, we'd need to know why Form2 needs to access Form1's DataGridView. It's likely that, instead, they should both access a shared back-end resource. Maybe something more like this:

    public class Form1
    {
        private DataGridView dgv { get; set; }
    
        private void LoadMyData()
        {
            dgv.DataSource = GlobalDataSources.SomeDataSource;
        }
    
        private void DoSomething()
        {
            var anotherForm = new Form2();
            anotherForm.DoSomethingElse();
        }
    }
    
    public class Form2
    {
        public void DoSomethingElse()
        {
            var data = GlobalDataSources.SomeDataSource;
        }
    }
    
    public class GlobalDataSources
    {
        private static SomeDataSourceType _someDataSource;
        public static SomeDataSourceType SomeDataSource
        {
            get
            {
                if (_someDataSource == null)
                {
                    // populate the data source
                }
                return _someDataSource;
            }
        }
    }
    

    As always, there are many ways to do it. But the basic idea is that, instead of accessing each other and creating all kinds of cross-dependencies, your front-end forms access shared back-end resources and the dependency chain just flows in that one direction.

    0 讨论(0)
  • 2021-01-27 13:27

    In Form2:

    public DataGridView Dgv { get; set; }
    

    In Form1:

    Form2 f = new Form2();
    f.Dgv = mainframe.Dgv;
    

    In Form2 access its own Dgv propety.

    0 讨论(0)
  • 2021-01-27 13:31

    You need to make the dataGridView field/property "public" or "internal". Choose public if you're a beginner like it sounds.

    0 讨论(0)
提交回复
热议问题