简体   繁体   English

如何将datagridview值传递给另一种形式

[英]how to pass datagridview value to another form

i am working with my c# windows aplication, in this application form named "patients" and other named "patientsDuplicatedName" which contain datagridview and load all Duplicated Patients Name (this and works fine,,) but i want when slsected Row get all values into form "patients" at run time (already open) without creating new form "Patients".. Below is the code I am referring to: 我正在使用我的c#Windows应用程序,在此应用程序表单中名为“ Patients”,其他名为“ Patients DuplicatedName”,其中包含datagridview并加载所有Duplicated Patient Name(此方法工作正常,),但我想在将横切的Row获取所有值时在运行时(已打开)创建“患者”表单,而无需创建新的“患者”表单。以下是我指的代码:

    public partial class frmPatientsNameDuplicated : Form
{
    PatientFiles frmPatientsFiles =new PatientFiles() ;
    public frmPatientsNameDuplicated()
    {
        InitializeComponent();
    }


   private void btnCancel_Click(object sender, EventArgs e)
   {
       this.Close();
   }

   private void btnOk_Click(object sender, EventArgs e)
   {

       frmPatientsFiles.txtFileNum.Text = this.dgvPatientsName.CurrentRow.Cells[0].Value.ToString();
       frmPatientsFiles.txtArbName.Text = this.dgvPatientsName.CurrentRow.Cells[1].Value.ToString();
       frmPatientsFiles.txtEngName.Text = this.dgvPatientsName.CurrentRow.Cells[2].Value.ToString();
       //frmPatientsFiles.show();//this line is creating new form and run 
       this.Close();

   }
}

sorry about my bad english & thanks in advance 对我的英语不好对不起,谢谢

The commented out line frmPatientsFiles.show() has a comment that says that the line is creating a new form. 注释掉的行frmPatientsFiles.show()有一条注释,指出该行正在创建新表单。 It is not. 它不是。 It is simply displaying a form that has been previously created on line PatientFiles frmPatientsFiles = new PatientFiles(); 它只是显示先前在PatientFiles frmPatientsFiles = new PatientFiles();行上创建的PatientFiles frmPatientsFiles = new PatientFiles(); This appears to be creating the new form that you don't want. 这似乎正在创建您不需要的新表单。 If you already have an existing form that you want to update, reference that form from your btnOk_Click event handler. 如果您已有要更新的现有表单,请从btnOk_Click事件处理程序中引用该表单。 To do this, you probably want to pass in a reference to the (existing) form to your class, either via the constructor or some other method/property. 为此,您可能想通过构造函数或其他方法/属性将对(现有)表单的引用传递给您的类。 I hope I have understood your question correctly. 希望我已正确理解您的问题。

i found same issue here : Pass data to a existing form so my code become 我在这里发现了同样的问题:将数据传递到现有表单,这样我的代码就变成了

public partial class frmPatientsNameDuplicated : Form
{
PatientFiles frmPatientsFiles = Application.OpenForms["PatientFiles"] as PatientFiles;
public frmPatientsNameDuplicated()
{
    InitializeComponent();
}
private void btnCancel_Click(object sender, EventArgs e)
{
   this.Close();
}

private void btnOk_Click(object sender, EventArgs e)
{
   frmPatientsFiles.txtFileNum.Text = this.dgvPatientsName.CurrentRow.Cells[0].Value.ToString();
   frmPatientsFiles.txtArbName.Text = this.dgvPatientsName.CurrentRow.Cells[1].Value.ToString();
   frmPatientsFiles.txtEngName.Text = this.dgvPatientsName.CurrentRow.Cells[2].Value.ToString();
   this.Close();
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM