Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法刷新datagridview-需要对象引用_C#_Winforms_Datagridview - Fatal编程技术网

C# 无法刷新datagridview-需要对象引用

C# 无法刷新datagridview-需要对象引用,c#,winforms,datagridview,C#,Winforms,Datagridview,每次form2关闭时,我都试图刷新form1上的datagridview 在表单2中,我有一个close事件处理程序,它调用静态方法RefreshGridView(位于表单1中) 在表单1中,我有一个静态方法RefreshGridView来刷新网格,如下所示 public static void RefreshGridView() { BindingSource bs = new BindingSource(); bs.DataSource = dataGridView1.Dat

每次
form2
关闭时,我都试图刷新
form1
上的datagridview

在表单2中,我有一个close事件处理程序,它调用静态方法RefreshGridView(位于表单1中)

在表单1中,我有一个静态方法RefreshGridView来刷新网格,如下所示

public static void RefreshGridView()
{
    BindingSource bs = new BindingSource();
    bs.DataSource = dataGridView1.DataSource;
    dataGridView1.DataSource = bs;
}
但是,每次尝试运行代码时,我都会在以下行中收到这些错误消息:

bs.DataSource = dataGridView1.DataSource;
错误:非静态字段、方法或属性“PGPTool.Form1.dataGridView1”需要对象引用

dataGridView1.DataSource = bs;
错误:非静态字段、方法或属性“PGPTool.Form1.dataGridView1”需要对象引用

dataGridView1.DataSource = bs;

如何解决此问题?

RefreshGridView
不应是静态方法,而应是实例方法。而对
form1
实例的引用应提供给
form2
。这是因为您希望此方法对
form1
上的元素进行更改,这些元素是该实例的一部分。(例如,如果您有两个
form1
的实例,静态方法将不知道要修改哪个。或者,如果您没有实例,则不会有任何要修改的内容。)

因此,应将方法更改为:

public void RefreshGridView()
form2
中,您应该有一种方法来保存对
form1
的引用。大概是这样的:

private Form1 Form1Instance { get; set; }
public Form2(Form1 form1Instance)
{
    // perform any error checking and/or null checking on form1Instance here

    Form1Instance = form1Instance;
}
//f1 is the instance of Form1
Form2 f2 = new Form2();
f2.FormClosed += (o, e) => { f1.RefreshGridView(); };
//show f2 or do whatever else you need to do with it
既然您希望在每次
form2
关闭时调用此引用上的某些内容,那么可以安全地假设
form2
每次创建时都需要引用
form1
?如果是这样,那么您可以在
form2
的构造函数中强制执行。大概是这样的:

private Form1 Form1Instance { get; set; }
public Form2(Form1 form1Instance)
{
    // perform any error checking and/or null checking on form1Instance here

    Form1Instance = form1Instance;
}
//f1 is the instance of Form1
Form2 f2 = new Form2();
f2.FormClosed += (o, e) => { f1.RefreshGridView(); };
//show f2 or do whatever else you need to do with it
然后在
form2
上的事件中,您可以在该实例上调用该方法:

Form1Instance.RefreshGridView();

如果您不希望
Form2
引用
Form1
,还有另一种方法:事件侦听器。只有当
Form1
负责创建
Form2
的实例,或者某些中心代码负责创建这两个实例时,这些方法才有效

下面是您将如何操作(如果
Form1
正在创建
Form2
的实例):

如果其他类正在执行此操作,则必须按如下方式修改:

private Form1 Form1Instance { get; set; }
public Form2(Form1 form1Instance)
{
    // perform any error checking and/or null checking on form1Instance here

    Form1Instance = form1Instance;
}
//f1 is the instance of Form1
Form2 f2 = new Form2();
f2.FormClosed += (o, e) => { f1.RefreshGridView(); };
//show f2 or do whatever else you need to do with it

正如我在评论中提到的和David在他的评论中提到的,您需要修改
RefreshGridView()
方法的签名以删除
static
关键字。

将表单2视为一个对话框,这样做会更简单。处理完
表单2
后,调用
对话框result.OK
。它还将关闭它

如果form1 datagridview已绑定到bs bindingsource,则可能不需要创建新的bindingsource,只需再次查询数据即可

public void RefreshGridView()
{
    var q = //your query

    bs.dataSource = q
}
回到
Form1
你有这样的东西,每次你想打开
Form2
时都会调用它:

Form2 f2 = new Form2();
f2.FormClosed += (o, e) => { RefreshGridView(); };
//show f2 or do whatever else you need to do with it
private void openForm2()
{
    var f2 = new Form2(); //create new form2
    var formResult = f2.ShowDialog(); //open as Dialog and check result after close event
    if (formResult == DialogResult.OK) //check form2 dialog result
    {
        //form2 gave OK, I can update my DGV and display msg
        MessageBox.Show("DGV will be updated", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
        //update my DGV
        RefreshGridView();
    }
    else
    {
        //form2 passed Cancel or something else, not good
        MessageBox.Show("Form2 Closed but nothing happened.", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }
}   

无法从静态方法访问实例字段(在本例中为
dataGridView1
)。
Form2
是否没有引用
Form1
?是
Form1
负责创建
Form2
的实例,还是其他类负责?