简体   繁体   中英

How to Transfer data from datagridview to the active form?

I am working on GYM Desktop application. Here with Product Entry form I insert the data in database table. And when I click the 'get data' button all the records appear in different form (Product_List) in datagridview. Now on double clicking on any particular row(in Product_List form) I want to get the data of that particular row in my currently active Product_Entry Form, so i can update or delete the data. so how can i get the data of that row on currently active Product Entry form ?

here is the code on mouse double_click on row in datagridview:

  private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs 
 e)
    {

        Product_Entry pe = new Product_Entry();
        pe.textBox1.Text = 
 dataGridView1.CurrentRow.Cells[0].Value.ToString();
        pe.textBox2.Text = 
 dataGridView1.CurrentRow.Cells[1].Value.ToString();
        pe.comboBox1.Text = 
 dataGridView1.CurrentRow.Cells[2].Value.ToString();
        pe.comboBox2.Text = 
 dataGridView1.CurrentRow.Cells[3].Value.ToString();
        pe.textBox3.Text = 
 dataGridView1.CurrentRow.Cells[4].Value.ToString();
        pe.textBox4.Text = 
 dataGridView1.CurrentRow.Cells[5].Value.ToString();
        pe.textBox5.Text = 
 dataGridView1.CurrentRow.Cells[6].Value.ToString();
        pe.textBox6.Text = 
 dataGridView1.CurrentRow.Cells[7].Value.ToString();
        pe.ShowDialog();

    }

产品条目表

产品清单表格

Use of global variable may solve this. declare a global variable like public static string ProductId= ""; in your datagridview form. Then on dataGridView1_MouseDoubleClick event set your productId like

private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        ProductId = dataGridView1.CurrentRow.Cells[0].Value.ToString();
        Form2 frm2 = new Form2(); //your second form
        frm2.Show(); 
    }

Then in second form (where you want to show the value) receive the ProductId in Form2_Load event like

   private void Form2_Load(object sender, EventArgs e)
    {
        label1.Text = Form1.ProductId; //label is your control to show data
    }

Like this you can receive all data in second form or you can write a database query with productid to fetch data from database . For more see this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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