Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

Calculate difference in fields when user inputs

Hi
I have 6 fields as follows on a ASP.net datagridview as follows.
The fields are for entering the number of days in two periods, say called  X & Y or to calculate the difference in the two period - the user needs both options.
1) Number of Days X
2) Number of Days Y
3) Start Date X
4) End Date X
5) Start Date Y
6) End Date Y

The user can enter "Number of X" & "Number of Y"

or choose a start and end date and have fields 1) & 2) calculated automatically.

Fields 3-6 give ajax popup calendars.

IF the user chooses a start and end date in fields 3) & 4), field 1) should be calculated as field 4)-field 3)
IF the user chooses a start and end date in fields 5) & 6), field 2) should be calculated as field 6)-field 5)

I'm stuck at how to trigger the calcs of field 1) & field 2)  when user makes change to fields 3-6.

The number of rows can change when user clicks on an "Add" button. This is an inmemory dataset, it is not linked to a database.

I presume there is some javascript I could add or something and it needs to be added as each row is added?  Or should it be done server side?

Thanks in advance for code to solve this problem.
0
rwallacej
Asked:
rwallacej
  • 5
  • 3
1 Solution
 
BadotzCommented:
This can be done on the client using event sinks.

Add "onchange=calc_1();" to field 4 and "onchange=calc_2();" to field 6

function $(id) { return document.getElementById(id); } // shorthand method

function calc_1() {
    var from_date = $('START_DATE_X');
    var thru_date = $('END_DATE_X');
    if (!from_date) { alert('Invalid "from" date'); return false; }
    if (!thru_date) { alert('Invalid "thru" date'); return false; }
    var num_days = thru_date - from_date; // this may need to be altered, depending upon where the value is
    $('Number_of_days_X').value = num_days;
    return num_days;
}

function calc_2() {
    var from_date = $('START_DATE_Y');
    var thru_date = $('END_DATE_Y');
    if (!from_date) { alert('Invalid "from" date'); return false; }
    if (!thru_date) { alert('Invalid "thru" date'); return false; }
    var num_days = thru_date - from_date; // this may need to be altered, depending upon where the value is
    $('Number_of_days_X').value = num_days;
    return num_days;
}

You can further optimize the functions, combing them into one, if you wish.

Please note the functions are untested, and bugs may exist.
0
 
BadotzCommented:
Of course, the statement in "calc_2" shown as:

$('Number_of_days_X').value

should be:

$('Number_of_days_Y').value
0
 
rwallacejAuthor Commented:
Hi BadotzDate

Thanks for feedback

Before I commence, please advise how the line
var from_date = $('START_DATE_X');  
(and subsequent like this)
works?

How does javascript know which row of the datagridview I am on to be able to get the correct text field?

I understand the return document.getElementById(id);  but I'd think that different rows on the gridview will have different IDs for the "Number of Days X" field

Thanks
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
BadotzCommented:
I do not know where the selected day value is with the calendar object; that is what you have to find out.

And are you saying that fields 1 and 2 are repeated in a grid? If that is the case, then yes, you will have to pull out the calendar object values for each row, and stuff the values for fields 1 and 2 in a similar manner.

0
 
rwallacejAuthor Commented:
BadotzDate,

all fields are repeated in grid - this is the problem with getting values by their id
0
 
BadotzCommented:
You will have to trap the "onchange" event for the grid, and determine - by interrogating the event object - what element in the grid triggered the event. If it is eiter of the thru dates, then you can proceed, otherwise not.

You should be able to use the column of the cell to determine what triggered the event, since giving every cell a unique id attribute seems like overkill.

Again, these are untested, and bugs may exist. The logic seems correct; I apologize for my lack of .NET knowledge.

function resolve_event(ev) { 
    var evnt = { evt: null, tgt: null };
    if (ev === undefined) { ev = window.event; }
    evnt.evt = ev || window.event; 
    evnt.tgt = evnt.evt.target || evnt.evt.srcElement; 
    if (evnt.tgt.nodeType == 3) { // defeat Safari bug 
        evnt.tgt = evnt.tgt.parentNode; 
    }
    return evnt;
}
 
function grid_change(e) {
    //
    // eobj	JSON object containing the event "target" and the parent event
    // col	column of grid for end date "X" or "Y"
    // row	row of grid for end date "X" or "Y"
    // g	grid object
    // c	column for # days (1 or 2)
    //
    eobj = resolve_event(e);
    var col = Number(eobj.tgt.column); // syntax may be wrong for "column"
    if (col == 4 || col == 6) { 
        var c = (col / 2) - 1;
        var g = $('YOUR_GRID_ID');
        r = eobj.tgt.row; // syntax may be wrong for "row"
        g.row[row].col[c].value = g.row[row].col[col].value - g.row[row].col[col - 1].value;
}

Open in new window

0
 
rwallacejAuthor Commented:
thank-you very much; this has helped me in getting solution
regards,
rwallacej
0
 
BadotzCommented:
No worries - glad to help.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now