Click here to Skip to main content
13,045,790 members (46,050 online)
Rate this:
Please Sign up or sign in to vote.
See more: , +
Hi all,

I have started playing with Roslyn, but have gotten stuck.
(This is just a proof of concept, and I'm also not 100% sure if I am going about this the right way.)

I am trying to create a method that will calculate values based on a dynamic calculation string, by utilizing the values from a passed DataGridViewRow. At the end of the day, this must be able to run 5 calculations per DataGridViewRow (500 row in total) without taking forever.


I have created a simple object to carry the values:

public class calculationObject
   public string calc { get; set; 
   public DataGridViewRow row { get; set; }

The string calculation is: (it is stored as a string in my calcutationObject):
//just a test calculation using decimal parsing
(decimal.Parse(row.Cells["productBasePrice"].Value.ToString()) + decimal.Parse(row.Cells["productBasePrice"].Value.ToString()) ).ToString()

The methods I have created to do the calculation:

public static MetadataReference hostReference;
public static ScriptEngine engine;
public static void setup()
   hostReference =
   new AssemblyFileReference(typeof(frmImportData).Assembly.Location);
   engine = new ScriptEngine(
   importedNamespaces: new[] { "System", "System.Text", "System.Windows.Forms" });
public static string trail(calculationObject co)
   setup(); /*create Roslyn objects*/
   var code = @"DataGridViewRow row = " + co.row + @";
   int test = " + co.calc + @"; test.ToString();";
   var session = Session.Create();
   return engine.Execute<string>(code, session);

I call these methods as such:
calculationObject co = new calculationObject();
co.calc = cal;
co.row = dgvDatabase.Rows[0];
string test = trail(co);


The problem I now seem to have, is that the DataGridViewRow row are not being referenced properly by the trail method. The "code" var's value is:

DataGridViewRow row = DataGridViewRow { Index=0 };
int test = (decimal.Parse(row.Cells["productBasePrice"].Value.ToString()) + decimal.Parse(row.Cells["productBasePrice"].Value.ToString()) ).ToString(); test.ToString();


So I guess, what I am trying to figure out, is how to reference the col.row object in the "code" var, in order to have the calculation utilize that values.

Thank you!
Posted 4-Jul-13 9:22am
Updated 4-Jul-13 9:24am

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 4 Jul 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100