在c#中将值从一个表单传递到另一个表单

我必须在C#中将id从一个表单传递到另一个表单。

我无法做到这一点。

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"); } } } 

谁能告诉我我做错了什么?

在Form1中

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

在Form2中

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

看到这个代码,我想这会对你有帮助。

AddInvoiceAddInvoice一个属性:

 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"); } } 

只需在AddInvoice用作CellValue

哦,如果这实际上是代码,我想你可能就是this.Hide(); 而不是创建一个新的NonPaideData并隐藏它。