Click here to Skip to main content
15,349,320 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
SQL
Hi Guys

Any example on how to convert a datatable with 4 rows and 4 columns from string to double so I could add the rows to a double type datatable ? thanks
Posted
Comments
Sergey Alexandrovich Kryukov 30-Jan-15 23:11pm
   
How about working without those trash examples? Why not looking at appropriate TryParse method in original MSDN documentation (at least to spell it the way it's written in the library :-), look at the usage and sample and actually write what you need?

Think at this:
What have you tried so far?

—SA

1 solution

Pretty easy to make this a generic method:
C#
// required
using System.Data;

private DataTable ConvertDataTable<T>(DataTable sourceTable)
{
    DataTable clone = sourceTable.Clone();

    for (int i = 0; i < clone.Columns.Count; i++) clone.Columns[i].DataType = typeof(T);

    foreach (DataRow row in sourceTable.Rows) clone.ImportRow(row);

    return clone;
}

// usage
// private DTAsDouble;
// DTAsDouble = ConvertDataTable<double>(currentStringDataTable);
   
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900