Hello everybody,

Let me explain my problem. I have one datatable with all my data and the above function to calculate the eclidian distance.

My function returns a DataColumn so I can add this column later to another datatable.

The problem is that I´m not being able to populate the datatable whit the Table.NewRow() method?

Can anyone help me?

Thanks in advance!

public DataColumn distanciaEclidiana(DataTable dt, DataRow distRefRow, List<string> campos)
        {
          

            DataColumn Dist = new DataColumn();

            Dist.DataType = typeof(float);

            Dist.ColumnName = "Dist";

            foreach(DataRow dr in dt.Rows)
            {
                float soma = 0;
                foreach (string col in campos)
                {

                    soma += (float)Math.Pow(Convert.ToDouble(distRefRow[col]) - Convert.ToDouble(dr[col]), 2);

                }
	            // The problem is here
                    DataRow row = Dist.Table.NewRow();

                    row["Dist"] = (float)Math.Sqrt(soma);
            }

            return Dist;
        }

You cannot add rows to dataColumn. You can ONLY add then to dataTable. DataColumn is only a "part" of dataTable, as it is Datarow.
So what i suggest you, is that you add new column to dataTable, and then add data to this column (you do NOT need to add rows, because rows already exists).

If you dont want to add column to existing datatable, create a NEW dataTable, and then create a new datacolumn (as you already did) and then use table.NewRow() method.

Example:

public DataTable distanciaEclidiana(DataTable dt, DataRow distRefRow, List<string> campos)
{
    DataTable newTable = new DataTable("OneColumnTable");
    table.Columns.Add("Dist", typeof(float));
    DataRow row;
    foreach(DataRow dr in dt.Rows)
    {
        float soma = 0;
        foreach (string col in campos)
        {
            soma += (float)Math.Pow(Convert.ToDouble(distRefRow[col]) - Convert.ToDouble(dr[col]), 2);
        }
        row = table.NewRow();
        row["Dist"] = (float)Math.Sqrt(soma);
    }
    return table;
}

Now you will be reaturning DataTable!!! Dont forget to change the code on returning.

I am not sure what you need some column, but you can simple add data from the created dataTable to some other.

I hope this it it,
bye

Ok, this works for me but I´m trying to figure out a way to copy an entire column from one datatable to another.

What I was thinking with this funcition is something like, add the distanteces to a column and return this column so I can add to a datatable.

Ok, I undertood that I can´t do that whith a DataColumn I need to use a DataTable, but now my question goes:

What is the best way to copy a column of one datatable to another, considering that the datatables has that same row size? Doing a foreach loop ?

Thanks in advance!

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.