2015-04-01 157 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... 
    } 
相關問題