Data conversion, manipulation & transfer

charlie20

Member
Joined
Aug 27, 2012
Messages
11
Programming Experience
Beginner
Hia all,

Hoping you're all having a great festive season !!

Quick question to pose :-

I've a DataTable that has a single column and houses data in string format (as imported from external files). I'm now moving this data to a secondary DataTable and would like to apply some conversion / manipulation in the process.

I'd like to do the following :-
- Convert a column in DataTable2 to Decimal
- Read the string data from DataTable1, convert it to a decimal datatype, multiply the resulting value by 100 and apply the Absolute mathematical function.
- Insert the result into DataTable2

I can do each of the functions (multiplication, Absolute, etc.) independantly on static data (ie:- not dynamic data from a DataTable) and can copy data back & forth between the DataTables. The issue I have is doing all in a single step.
ie:-

foreach (DataRow row in DataTable1.Rows)
{
decimalVal = System.Convert.ToDecimal(row);
decimalVal = (decimalVal * 100);
abs = Math.Abs(decimalVal);
DataTable2.ImportRow(abs);
}

Naturally the above does not work but hopefully gives an idea as to what I'm attempting to do.
Any help / pointers would be really useful.

Thanks
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,219
Location
Sydney, Australia
Programming Experience
10+
The whole point of ImportRow is to import a row. In your code, 'abs' is a Decimal, not a DataRow. 'row' is the DataRow. If you want to use ImportRow then you must modify the data in the original DataRow and then import that row. Otherwise, you need to call Rows.Add to add a new row. As the original column contains Strings and the destination contains Decimals, ImportRow is not an option.
 

charlie20

Member
Joined
Aug 27, 2012
Messages
11
Programming Experience
Beginner
Not sure why this has been moved to the ADO.NET thread as I'm not using ADO ??

Thanks jmcilhinney - it was a mistake in my code example rather than my app. Discovered that I had to format the string first. All working now :)
 
Last edited:
Top Bottom