2015-04-01 119 views
-1

我的程序中有一个搜索表单。当用户在搜索表单上双击(dgv的)单元格时,我希望程序关闭该表单并跳转到主表单上的项目。将值从一种形式传递给另一种(C#)

我这样做是通过识别具有唯一ID的每个项目。

我试图将行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 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 
+0

让id字段的性质和主要形式从中获取价值的 – 2015-04-01 16:19:25

+0

可能重复[如何在C#Windows应用程序形式之间传递值? ](http://stackoverflow.com/questions/1205195/how-to-pass-values-between-forms-in-c-sharp-windows-application) – Orace 2015-04-01 16:19:32

+0

@Orace这不是重复的,因为我用过这个方法之前和它的工作。但由于某种原因,它现在不工作。 – John 2015-04-01 16:20:53

回答

3

我建议你做如下:

搜索表单:

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... 
    } 
相关问题