0

I have two DataTables. I simply wanna save one of them in the other and then change the column name on my "temporary table".

My problem is that everytime that I change the column name from one table, it is automatically changing in the other.

declaration:

public DataTable DataTablefromDataBaseFormDataType { get; private set; }

How do I get value for the first DataTable:

DataTablefromDataBaseFormDataType = ((FormProject)Fproject).DataTablefromDataBaseFormProject;

How do I copy and change column name:

        DataTableUsedToPlotGraphTemp = DataTablefromDataBaseFormDataType;

    DataTableUsedToPlotGraphTemp.Columns[column_to_use].ColumnName = "temp";

Rest of the code (if needed)

    DataTableUsedToPlotGraphTemp = DataTablefromDataBaseFormDataType;

    DataTableUsedToPlotGraphTemp.Columns[column_to_use].ColumnName = "temp";

// This point! - When I change DataTableUsedToPlotGraphTemp it is also changing //DataTableUsedToPlotGraphTemp

    if (ReceiveStartDate == null && ReceiveEndDate == null)
    {
        ReceiveStartDate = DataTableUsedToPlotGraphTemp.Rows[0][4].ToString();
        ReceiveEndDate = DataTableUsedToPlotGraphTemp.Rows[DataTableUsedToPlotGraphTemp.Rows.Count-1][4].ToString();
    }

    if (ReceiveNameFile != "All files")
    {


        string query = $"TIME_FORMATTED >='{ReceiveStartDate}' AND TIME_FORMATTED <='{ReceiveEndDate}'";
        DataRow[] result = DataTableUsedToPlotGraphTemp.Select(query);


        foreach (DataRow row in result)
        {
            //MessageBox.Show(row[0].ToString());
            DataTableUsedToPlotGraph.ImportRow(row);
        }

    }
    else
    {
        DataRow[] result = DataTablefromDataBaseFormDataType.Select("FILE_NAMES = '"+ ReceiveNameFile + "'");

        foreach (DataRow row in result)
        {
            //MessageBox.Show(row[0].ToString());
            DataTableUsedToPlotGraph.ImportRow(row);
        }
    }



}

I already debugged several times to see if it is happening in some other place of the code. The moment that it changes the column name, it changes for both DataTables.

Can anybody help me? Thank you.

Fabio Soares
  • 101
  • 2
  • 12

1 Answers1

2

You don't have two different DataTables(reference type) but only one:

DataTableUsedToPlotGraphTemp = DataTablefromDataBaseFormDataType;

So if you change one you are changing both. Those are just variables that reference the same object. You can use DataTable.Copy:

DataTable DataTableUsedToPlotGraphTemp = DataTablefromDataBaseFormDataType.Copy();

If you just need an empty table with the same columns you can use DataTable.Clone.

Maybe this explains it a little bit better:

For a value type, the value is the information itself. For a reference type, the value is a reference which may be null or may be a way of navigating to an object containing the information.

For example, think of a variable as like a piece of paper. It could have the value "5" or "false" written on it, but it couldn't have my house... it would have to have directions to my house. Those directions are the equivalent of a reference. In particular, two people could have different pieces of paper containing the same directions to my house - and if one person followed those directions and painted my house red, then the second person would see that change too. If they both just had separate pictures of my house on the paper, then one person colouring their paper wouldn't change the other person's paper at all.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939