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 .
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.
Re: calculate the value of one cell by values of 2 other cells
Quote:
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
Re: calculate the value of one cell by values of 2 other cells
Quote:
i want define it after running the program
Then you're going to have to write an equation parser which is decidedly advanced coding.
Re: calculate the value of one cell by values of 2 other cells
Quote:
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?
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 %.
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.