-
Apr 6th, 2013, 09:48 AM
#1
Thread Starter
Lively Member
calculate the value of one cell by values of 2 other cells
hi
i have a datagridview in my form . in very simple state : i want to set the value of cell(1) of rows(1) equal to sum of values of cell(2) and cell(3) of rows(1) ... in other word :
Code:
Dgv1.Rows(1).cell(1).value=Dgv1.Rows(1).cell(2).value + Dgv1.Rows(1).cell(3).value
my problem is that i want each time i input a value in cells(2) or cells(3) then the value of cells(1) change to result .
please think about a large size of data.this example is very simple .
-
Apr 6th, 2013, 11:25 AM
#2
Re: calculate the value of one cell by values of 2 other cells
What problems are you having? Are you getting an error, if so, where?
That said, there are two DGV events that could help you update cell(1). "CellEndEdit", "CellValueChanged". Put the code for updating cell(1) in one of these events.
-
Apr 6th, 2013, 12:36 PM
#3
Thread Starter
Lively Member
Re: calculate the value of one cell by values of 2 other cells
Originally Posted by wes4dbt
What problems are you having? Are you getting an error, if so, where?
That said, there are two DGV events that could help you update cell(1). "CellEndEdit", "CellValueChanged". Put the code for updating cell(1) in one of these events.
i want to create a program like excel . (but only a part of excel ) you can run excel and then define a equation for a cell .
i want after run my program i specify the equation of one cell (my first example ) .
the main problem is that i want define it after running the program . i said cell(5)=cell(3)+cell(4) .... i want select cell(3) and cell(4) after running the program . like excel
-
Apr 6th, 2013, 02:47 PM
#4
Re: calculate the value of one cell by values of 2 other cells
i want define it after running the program
Then you're going to have to write an equation parser which is decidedly advanced coding.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Apr 6th, 2013, 03:27 PM
#5
Thread Starter
Lively Member
Re: calculate the value of one cell by values of 2 other cells
Originally Posted by dunfiddlin
Then you're going to have to write an equation parser which is decidedly advanced coding.
exactly ... i wrote it before ... it get an expression and solve it like excel. for example in this function if input experssion=(((334*45)/5*2+(254-23)^2)) then output will be :59373
i can replace the numeric values with variables and send it to function to solve.
but my problem is that i said. how can i do this?
Last edited by pooya1072; Apr 6th, 2013 at 03:42 PM.
-
Apr 7th, 2013, 09:55 AM
#6
Re: calculate the value of one cell by values of 2 other cells
As .paul. suggested in this thread, the DataTable has a Compute method that can do some expression parsing. I haven't tested how extensive it is, but it might be a very simple solution to your problem (unless you feel like writing an expression parser - which isn't all that difficult to do; numerous examples available online).
Tom
#EDIT: I can't quite figure out which operator is used for exponention - suffice to say it isn't ^. Modulo is supported as c-notation %.
Last edited by ThomasJohnsen; Apr 7th, 2013 at 10:14 AM.
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
-
Apr 7th, 2013, 02:45 PM
#7
Re: calculate the value of one cell by values of 2 other cells
You could take advantage of a DataTable, setup a data column with an expression or work thru the DataTable events.
Simple VS2010 example project.
The example project allows a user to load a DataGridView via a DataTable and BindingSource, you could simply use the DataTable if so desired.
I do not try to get into parsing, just provide to fixed expressions. The expressions could come from a text file or other data source.
Nothing done past simple assertion.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|