Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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#)_C#_Winforms_Forms_Datagridview - Fatal编程技术网

将值从一种形式传递到另一种形式(C#)

将值从一种形式传递到另一种形式(C#),c#,winforms,forms,datagridview,C#,Winforms,Forms,Datagridview,我的程序中有一个搜索表单。当用户双击搜索表单上的单元格(dgv)时,我希望程序关闭该表单并跳转到主表单上的项目 我用一个唯一的id标识每个项目 我试图将行id的值传递给另一个窗体。问题是,它说我每次都传递值0。但是,当我在搜索表单上插入一些消息框时,它表示整数“id”已成功分配给主表单上的变量:public int CellDoubleClickValue{get;set;} 这是我的密码: 搜索表格: private int id; private void searchdg

我的程序中有一个搜索表单。当用户双击搜索表单上的单元格(dgv)时,我希望程序关闭该表单并跳转到主表单上的项目

我用一个唯一的id标识每个项目

我试图将行id的值传递给另一个窗体。问题是,它说我每次都传递值0。但是,当我在搜索表单上插入一些消息框时,它表示整数“id”已成功分配给主表单上的变量:
public int CellDoubleClickValue{get;set;}

这是我的密码:

搜索表格:

    private int id;

    private void searchdgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        this.rowIndex1 = e.RowIndex;
        this.id = Convert.ToInt32(this.searchdgv.Rows[this.rowIndex1].Cells["id"].Value);
        invmain inv = new invmain();
        inv.CellDoubleClickValue = this.id;
        this.DialogResult = DialogResult.OK;
        this.Close();
        //MessageBox.Show(inv.CellDoubleClickValue.ToString()); 
        //Above, it shows it got assigned successfully.
    }
public int SelectedId { get; set; }

private void searchdgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    this.rowIndex1 = e.RowIndex;
    this.SelectedId = Convert.ToInt32(this.searchdgv.Rows[this.rowIndex1].Cells["id"].Value);
    this.DialogResult = DialogResult.OK;
    this.Close();
}
主要形式:

    public int CellDoubleClickValue { get; set; }

    private void searchToolStripMenuItem_Click(object sender, EventArgs e)
        {
        search form1 = new search();
        form1.ShowDialog();

        if (form1.DialogResult == DialogResult.OK)
        {
            MessageBox.Show(CellDoubleClickValue.ToString());
        }//Here it shows that the value is: 0

我建议你做如下工作:

搜索表格:

    private int id;

    private void searchdgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        this.rowIndex1 = e.RowIndex;
        this.id = Convert.ToInt32(this.searchdgv.Rows[this.rowIndex1].Cells["id"].Value);
        invmain inv = new invmain();
        inv.CellDoubleClickValue = this.id;
        this.DialogResult = DialogResult.OK;
        this.Close();
        //MessageBox.Show(inv.CellDoubleClickValue.ToString()); 
        //Above, it shows it got assigned successfully.
    }
public int SelectedId { get; set; }

private void searchdgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    this.rowIndex1 = e.RowIndex;
    this.SelectedId = Convert.ToInt32(this.searchdgv.Rows[this.rowIndex1].Cells["id"].Value);
    this.DialogResult = DialogResult.OK;
    this.Close();
}
主要形式:

private void searchToolStripMenuItem_Click(object sender, EventArgs e)
{
    search form1 = new search();
    form1.ShowDialog();

    if (form1.DialogResult == DialogResult.OK)
    {
        int selectedId = form1.SelectedId;
        // Do whatever with selectedId...
    }

在main form中生成id字段的属性并从中获取值@Orace的可能重复项这不是重复项,因为我以前使用过这个方法,而且它是有效的。但是由于某些原因,它现在不起作用了。@John我给了你一个可行的解决方案,但我认为它对你的困惑没有帮助。您对表单工作方式的思维模式不正确。首先,有一个“main”表单的实例,它是用代码创建的,您在这里没有显示。我相当肯定这是在应用程序启动代码中发生的。主窗体的实例依次创建并显示搜索窗体(在
searchToolStripMenuItem\u单击
事件处理程序中)。然后在搜索表单中的
searchdgv\u CellDoubleClick
事件处理程序中创建主表单的第二个实例。您显然认为声明
invmain inv=new invmain()创建对已打开的主窗体的引用,但它没有。这是一个单独的例子。