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 OriginalGriff 190
1 PIEBALDconsult 150
2 BillWoodruff 148
3 DamithSL 140
4 Jochen Arndt 125
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 4 Jul 2013
Copyright © CodeProject, 1999-2014
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