how to copy/transfer values from form2 datagridview to form1 datagridview in C#..?

后端 未结 4 1014
孤街浪徒
孤街浪徒 2021-01-25 03:39

can anyone do help..?

i really need help on this, here i have two forms [form1 & form2]. I have created datagridview(DGV) in each forms. Now i need to transfer/copy

4条回答
  •  悲哀的现实
    2021-01-25 04:22

    Declare Form1's DGV as Public instead of private and do the following in Form2 :

    1. Declare the following variable :

      System.Windows.Forms.Form f = System.Windows.Forms.Application.OpenForms["Form1"];.

    2. Do the Following changes in your program:

       private void button1_Click (object sender, EventArgs e)      
       {
      
       try
        {
          if (dataGridView1.RowCount != 0)
          {
      
              foreach (DataGridViewRow row in dataGridView1.SelectedRows)
              {                                       
                  ((Form1)f).dataGridView1.Rows.Add(row);                    
              }
      
            }
          else
          {
              MessageBox.Show("There is no data to export, please verify..!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
          }
      }
      catch { }   }
      

提交回复
热议问题