0

It could be silly question but it is bothering me. So I am passing datatable which is populated in the form1 and assigned to the data grid.

considering in mind that my_dt is already assigned to data grid as datasource.

form1 call form2

form2 form = new form2(my_dt);
form.show();

Form2

 public partial class form2 : Form
 {
    Datatable table;

    public form2(Datatable dt) 
    {
      table = dt;
    }
 }

in this form later on I am changing the column names of table and adding more columns to it. when I am calling the function which is changing the table columns when it is finished it is changing the table columns in data grid. The form2 is still open and the datagrid is in form1.

Why it is happening?

rene
  • 41,474
  • 78
  • 114
  • 152
  • I am new in this platform the post did not come quite correct . I can answer questions if any one needs any information. thanks – Sheikh Umair Ahmad Oct 13 '20 at 11:00
  • 1
    Because you pass a reference around. If you want two distinct datables make a [Clone()](https://learn.microsoft.com/en-us/dotnet/api/system.data.datatable.clone?view=netcore-3.1). – rene Oct 13 '20 at 11:01
  • 1
    I think the answer to your question is probably quite a simple one, but it's really hard to follow what you're trying to do and indeed what the problem is. Can you format your question better? This is useful: [Ask] – WSC Oct 13 '20 at 11:01
  • You want Datatable on form1 to be independent of Datatable on form2? – maxc137 Oct 13 '20 at 11:18
  • Thanks everyone for response and sorry for my bad formatting. I am not asking for fix as @rene described i have made a clone and imported the rows into it and it did fix the problem. my question is when i am assigning the data table which i made in the new form2 and assigning the datatable which is coming from form1 in the constructor why it is not treating as a separate variable? – Sheikh Umair Ahmad Oct 14 '20 at 10:45

0 Answers0