C#如何通过datagridview在表单之间传递值?

C#如何通过datagridview在表单之间传递值?,c#,forms,datagridview,C#,Forms,Datagridview,我有表格,是这样的 nomor tabungan是自动生成的 问题是,我想通过datagridview从不同的形式获取nomor nasabah的值。请看下面的图片 有一个按钮ambil来检索nomor nasabah值,并将它们传递给nomor nasabahtextbox.text 我已经成功地从datagridview中获取了nomor_nasabah的值。如下图所示 如何将值传递给tambah tabungannomor nasabahtextbox.text? 我已将文本框修改器

我有表格,是这样的

nomor tabungan
是自动生成的
问题是,我想通过datagridview从不同的形式获取nomor nasabah的值。请看下面的图片

有一个按钮
ambil
来检索
nomor nasabah
值,并将它们传递给
nomor nasabah
textbox.text

我已经成功地从datagridview中获取了nomor_nasabah的值。如下图所示

如何将值传递给
tambah tabungan
nomor nasabah
textbox.text? 我已将文本框修改器设置为
public
,因此当我单击
ambil
按钮时,文本框将自动填充检索到的值

我该怎么做

我做了下面的代码,并询问它为什么不工作

这是
ambil
按钮代码

private void button2_Click(object sender, EventArgs e)
    {
        Tabungan.Tambah tambahtabungan = new Tabungan.Tambah();
        //nomornya is the retrieved value            
        tambahtabungan.textBox2.Text = nomornya;
    }
这是
cari
按钮代码,用于显示
getCustomer
表单

 private void button2_Click(object sender, EventArgs e)
    {
        if (getcustomer == null || getcustomer.IsDisposed)
        {
            getcustomer = new getNasabah();                
            getcustomer.Show();
        }
    }
你应该试试这个

首先在getCustomer表单上创建一个公共属性,如

public string nomornyaValue { get; set;}
 private void Cari_Click(object sender, EventArgs e)
 {
     /*getCustomer getCustomerForm = new getCustomer();
     if(getCustomerForm.ShowDialog() == DialogResult.OK)
     {
          textBox2.Text = getCustomerForm.nomornyaValue;
     }*/
    if (getcustomer == null || getcustomer.IsDisposed)
    {
        getcustomer = new getNasabah();                
    }
    if(getcustomer.ShowDialog() == DialogResult.OK)
    {
        textBox2.Text = getcustomer.nomornyaValue;
    }
 }
然后修改您的
ambil
按钮,单击类似事件,并将此属性设置为您的datagrid值

private void button2_Click(object sender, EventArgs e)
{
   nomornyaValue = nomornya;
   this.DialogResult = DialogResult.OK;
}
然后在
tambah tabungan
按钮
Cari
上单击call
getCustomer
formlike

public string nomornyaValue { get; set;}
 private void Cari_Click(object sender, EventArgs e)
 {
     /*getCustomer getCustomerForm = new getCustomer();
     if(getCustomerForm.ShowDialog() == DialogResult.OK)
     {
          textBox2.Text = getCustomerForm.nomornyaValue;
     }*/
    if (getcustomer == null || getcustomer.IsDisposed)
    {
        getcustomer = new getNasabah();                
    }
    if(getcustomer.ShowDialog() == DialogResult.OK)
    {
        textBox2.Text = getcustomer.nomornyaValue;
    }
 }

表单是如何创建的,何时显示?是否先显示getCustomer表单?他们在同一个项目中吗?如果您从Tabungan.Tambah表单打开getCustomer表单,那么当您创建getCustomer表单时,您可以将Tabungan.Tambah表单的实例传递给一个变量,当您单击ambil时,使用该引用获取文本框。您最初是如何填充DataGridView的?通常我认为修改网格数据绑定到的对象,然后简单地刷新网格更有意义。哪个按钮是
Button2
?@RomanoZumbé这是
ambil
按钮@seekerOfKnowledge不,我们必须先显示
tambah tabungan
表单,然后,当我点击
tambah tabungan
表单上的
cari
时。它显示
get customer
表单以检索
nomor nasabah
值检查此答案。啊,是的,展示对话。一点也不坏+1
simpan
按钮用于向数据库中插入查询。但是让我先试试这个谢谢!它可以工作,但我需要理解你的代码。这行“this.DialogResult=DialogResult.OK;”是为什么工作的?由于没有显示YesNo消息框?在Cari_Click方法中,行if(getcustomer.ShowDialog()==DialogResult.OK)显示getcustomer表单,并等待DialogResult。在按钮2_-Click(ambil)中,“this”引用了在Cari_-Click中打开的当前getCustomer表单,他将DialogResult设置为OK,这样在Cari_-Click方法中,它就可以在文本框中获取nomornyaValue。对话框结果的默认值是什么。如果我没有弄错,单击
ambil
按钮后,
对话框结果
值变为“OK”。那么,我们不需要再次按下
Cari_单击
来执行这一行吗如果(getcustomer.ShowDialog()==DialogResult.OK){textBox2.Text=getcustomer.nomornyaValue;}`