HI,

At the moment I'm facing a problem. I have a datagridview1 in FORM1 and datagridview2 in FORM2. I want to duplicate the data in datagridview1 to datqagridview2 in another form.

The code I used is:

FORM1:

OdbcCommand bcCom2 = new OdbcCommand();
 bcCom2.CommandText = "" + content + "";
 bcCom2.Connection = OdbcCon;
DataSet q2 = new DataSet();
OdbcDataAdapter dbA2 = new OdbcDataAdapter(bcCom2);
dbA2.Fill(q2);
dataGridView1.DataSource = q2.Tables[0];
datagrid1 = (DataTable)q2.Tables[0];

where content is a MySQL string and datagrid1 is a datatable.

FORM2:

private Form1 m_parent;
   
        DataTable t = new DataTable();
        public FORM2(Form1 frm1)
        {
            InitializeComponent();
            m_parent = frm1;
        }

        private void FORM2_Load(object sender, EventArgs e)
        {
            this.dataGridView2.DataSource = m_parent.datagrid1;
           
            dataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        }

At the moment it working fine but when I do some changes in datagridview2 it is also reflecting in datagridview1 of the main form (FORM1). Is it possible to make them independent as soon as the datagridview2 has the data?

Thanks,

Why you dont simply pass a reference of dataSet to Form2,and use it there as a dataSource of DGV2, the same as you did on form1 with DGV1?

But with one Exception: Make DataSet accessible for all Form1 class
Example:

//form1:
DataSet q2;

void PopulateDGV1()
{
   OdbcCommand bcCom2 = new OdbcCommand();
   bcCom2.CommandText = "" + content + "";
   bcCom2.Connection = OdbcCon;
   q2 = new DataSet();
   OdbcDataAdapter dbA2 = new OdbcDataAdapter(bcCom2);
   dbA2.Fill(q2);
   dataGridView1.DataSource = q2.Tables[0].DefaultView;
}

void OpenForm2()
{
    Form2 f2 = new Form2(q2);
    f2.Show();
}

//form2:
public Form2(DataSet _q2)
{
    dataGridView1.DataSource = _q2.Tables[0].DefaultView; //this is DGV on form2!!!
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.