1
votes

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 values from form2 datagridview to form1 datagridview by clicking one button on form2.

in form2 i wrote this..!

   private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (dataGridView1.RowCount != 0)
            {

                foreach (DataGridViewRow row in dataGridView1.SelectedRows)
                {
                    object[] items = new object[row.Cells.Count];
                    for (int i = 0; i < row.Cells.Count; i++)
                        items[i] = row.Cells[i].Value;
                    Form1 F1 = new Form1();
                    F1.Activate();
                    F1.dataGridView1.Rows.Add(items);
                    F1.dataGridView1.DataSource = items; 
                    dataGridView1.Rows.Remove(row);
                }

              }
            else
            {
                MessageBox.Show("There is no data to export, please verify..!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        catch { }

    }

and i'm getting error like "...dataGridView1.Form1 is inaccessible due to its protection level". Is there any other way or method to import values from form2 datagridview to form1 datagridview..?

any ideas...?

thanks in advance.

4

4 Answers

2
votes

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 { }   }
    
0
votes

You can simply declare the dataset/datatable which binds your form1 grid as a public and access it in form2. Via that datatable or dataset you can again bind grid on form2.

Or you can send dataset or datatable from form1 to form2(possibly through modular function) and then bind the grid.

This is the simplest way.

0
votes

You should use some kind of event for this purpose:

//the Form1 class
public partial class Form1 : Form {
  Form2 f2 = new Form2();  
  public Form1(){
    InitializeComponent();
    f2.TransferSelectedRows += (s,e) => {
      if (dataGridView1.RowCount > 0) {
         foreach(DataGridViewRow row in dataGridView1.SelectedRows){
           //add the selected row to the receiver grid
           e.ReceiverGrid.Rows.Add(row.Cells.OfType<DataGridViewCell>()
                                          .Select(cell=>cell.Value).ToArray());
           //remove the selected row
           dataGridView1.Rows.Remove(row); 
         }
         e.HasRows = true;
      } else {
        e.HasRows = false;
      }
    };
  }     
}
//Form2 class
public partial class Form2 : Form { 
  public Form2() {
    InitializeComponent();       
  }
  public class TransferSelectedRowsEventArgs : EventArgs {
    public DataGridView ReceiverGrid {get; private set;}
    public bool HasRows {get;set;}
    public TransferSelectedRowsEventArgs(DataGridView receiver){
       ReceiverGrid = receiver;
    }
  }
  public delegate void TransferSelectedRowsEventHandler(object sender, TransferSelectedRowsEventArgs e);
  public event TransferSelectedRowsEventHandler TransferSelectedRows;
  protected virtual void OnTransferSelectedRows(TransferSelectedRowsEventArgs e){
    TransferSelectedRowsEventHandler handler = TransferSelectedRows;
    if(handler != null) handler(this, e);
  }
  //Click event handler for button1
  private void button1_Click(object sender, EventArgs e){
    var transfer = new TransferSelectedRowsEventArgs(dataGridView1);
    OnTransferSelectedRows(transfer);
    if(!transfer.HasRows){
       MessageBox.Show("There is no data to export, please verify..!", "Error", 
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
  }
}

NOTE: The code I posted above is not complete, some members or code of your own should be added. You should also consider using DataSource for your Grid, that's is how we deal with DataGridView in real projects.

0
votes

Declare a public BindingList<row_object> in Form 2 and write it from Form 1. If the datagridview in Form 1 is bound to the bindingList, the the data will automatically refresh. See this to view how to bind a datagridview to a bindingList.