Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# DataGridView Roslyn
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 10:22am
Edited 4-Jul-13 10:24am
v2

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

  Print Answers RSS
0 Sascha Lefévre 485
1 Sergey Alexandrovich Kryukov 325
2 Maciej Los 230
3 Abhinav S 175
4 Peter Leow 110
0 Sergey Alexandrovich Kryukov 6,953
1 OriginalGriff 6,311
2 Maciej Los 2,732
3 Peter Leow 2,694
4 Abhinav S 2,652


Advertise | Privacy | Mobile
Web01 | 2.8.150414.1 | Last Updated 4 Jul 2013
Copyright © CodeProject, 1999-2015
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