C# 从另一个窗体访问datagrid

C# 从另一个窗体访问datagrid,c#,C#,我在form1中有datagridview。如何从form2访问datagridview private void button1_Click(object sender, EventArgs e) { string sql1 = "insert into Car (plate, color, [year], model) values ('"+tplate.Text+"','"+tcolor.Text+"',"+tyear.Text+",'"+tmodel.Text

我在form1中有datagridview。如何从form2访问datagridview

  private void button1_Click(object sender, EventArgs e)
  {
          string sql1 = "insert into Car (plate, color, [year], model) values ('"+tplate.Text+"','"+tcolor.Text+"',"+tyear.Text+",'"+tmodel.Text+"')";            
          string sql2 = "select * from Car";
            try
            {
                int res = CarDatabase.executeOthers(sql1);
                if(res >0){
                    DataTable dt = CarDatabase.executeSelect(sql2);
                    mainframe.Dgv.DataSource = dt;                
                    MessageBox.Show("New Car is added");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
  }
这里mainframe.Dgv是第一种形式


但是我不能像那样访问Form1.dataGridView为什么:S

您需要对实际的
表单的引用才能访问其成员。(这些成员必须是
公众人士

我只是说:

Form1.dataGridView
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;
    }
}
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;
        }
    }
}
不会工作,因为
Form1
只是一个类型,它不是对内存中实例化对象的引用。这就是引用
静态
成员的方式,这里不是这样。
DataGridView
是一个实例成员。因此,您需要引用
Form1
的实例。更像是:

firstForm.dgv
其中,
firstForm
Form2
上的一个变量(或作为参数从
Form1
传递到方法中,其中参数将是
this
,等等),
dgv
Form1
上的公共成员,表示
DataGridView

大概是这样的:

Form1.dataGridView
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;
    }
}
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;
        }
    }
}
注意,我在这里遗漏了很多WinForms的内容。那是故意的。这只是为了在代码级别演示这个概念。表单从何处继承,如何实例化,如何保存在内存中,这都是另一个问题

你如何设置这个取决于你。我并不精通WinForms开发,但我认为有更好的方法来实现这一点。为了确定这一点,我们需要知道为什么
Form2
需要访问
Form1
DataGridView
。相反,它们很可能都应该访问一个共享的后端资源。也许更像这样:

Form1.dataGridView
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;
    }
}
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;
        }
    }
}

一如既往,有很多方法可以做到这一点。但其基本思想是,前端表单访问共享的后端资源,而依赖链只是朝着这一方向流动,而不是相互访问并创建各种交叉依赖关系。

您需要将dataGridView字段/属性设置为“公共”或“内部”。如果您是初学者,请选择public(公共)。

在表格2中:

public DataGridView Dgv { get; set; }
在表格1中:

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

在Form2中访问其自己的Dgv属性。

Form1是否引用Form2?若否,可否告知本局?如果没有,其他表单或对象是否引用了Form1和Form2?在我的示例中,我有两个表单。在上面的网络示例中。如何从form2访问form1中的datagridview:sDoesForm2是否由form1打开?YeahForm2f=newForm2();f、 可见=真实;这是form1中的按钮单击事件