Results 1 to 7 of 7

Thread: calculate the value of one cell by values of 2 other cells

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2012
    Posts
    72

    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 .

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    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.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2012
    Posts
    72

    Re: calculate the value of one cell by values of 2 other cells

    Quote Originally Posted by wes4dbt View Post
    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

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2012
    Posts
    72

    Re: calculate the value of one cell by values of 2 other cells

    Quote Originally Posted by dunfiddlin View Post
    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.

  6. #6
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    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)

  7. #7
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    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
  •  



Click Here to Expand Forum to Full Width