2012-08-04 104 views
1

我必須在C#中將id從一個窗體傳遞到另一個窗體。將值從一個窗體傳遞到另一個窗體在c#

我無法做到這一點。

的C#代碼:

private void btnedit_Click(object sender, EventArgs e) 
{ 
    foreach (DataGridViewRow a in dataGridViewUnPaidList.Rows) 
    { 
     if (a.Cells[0].Value != null) 
     { 
      Convert.ToInt64(a.Cells[1].Value); // i have to pass this id in AddInvoice() form 
      AddInvoice ad = new AddInvoice(); 
      ad.Show(); 
      NonPaideData non = new NonPaideData(); 
      non.Hide(); 
     } 
     else 
     { 
     MessageBox.Show("Now Row Is Selected"); 
     } 
    } 
} 

誰能告訴我什麼,我做錯了什麼?

回答

3

在Form1

private void ShowForm2() 
{ 
    string value = TheTextBox.Text; 
    Form2 newForm = new Form2(); 
    newForm.TheValue = value; 
    newForm.ShowDialog(); 
} 

在窗體2

private string _theValue; 
public string TheValue 
{ 
    get 
    { 
     return _theValue; 
    } 
    set 
    { 
     _theValue = value; 
     // do something with _theValue so that it 
     // appears in the UI 

    } 
} 

看到這個代碼,我認爲這會幫助你。

5

使屬性在AddInvoice

public long CellValue { get; set } 

分配給它:

private void btnedit_Click(object sender, EventArgs e) 
{ 
    foreach (DataGridViewRow a in dataGridViewUnPaidList.Rows) 
    { 
     if (a.Cells[0].Value != null) 
     { 
      AddInvoice ad = new AddInvoice(); 
      ad.CellValue = Convert.ToInt64(a.Cells[1].Value); 
      ad.Show(); 

      NonPaideData non = new NonPaideData(); 
      non.Hide(); 
     } 
     else 
     { 
      MessageBox.Show("Now Row Is Selected"); 
     } 
    } 

,只是使用它作爲AddInvoiceCellValue

哦,如果這實際上是代碼,我想你可能意思是this.Hide();而不是創建一個新的NonPaideData並隱藏它。

相關問題