I have a WebDataGrid that is being used for something like a checking account register. It needs to maintain a running balance in the last column... which means all balances must be recalculated on every add/edit. I would prefer to do this without a postback.
is there a code sample that demonstrates how to loop through all the rows using the CSOM and read/modify cell values? I intend to use the same code to modify individual cell formatting (coloring negative amounts and balances red).
In case anyone else runs into a similar need... the following code does two things:
Just need to call "refreshBalances()" after each client-side grid update. There may be a better way to wire it to a grid event.
<style type="text/css">
.amountpositive
{
color: Green;
}
.amountnegative
color: Red;
.amount0
color: Black;
</style>
<script type="text/javascript">
function dgRegister_Initialize(sender, eventargs) {
refreshBalances();
function refreshBalances() {
var rows = $find("WebSplitter1_tmpl0_dgRegister").get_rows();
var rowlength = rows.get_length();
var rowindex = 0;
var currentAmount = 0;
var runningBalance = 0;
var currentrow;
for (rowindex = 0; rowindex <= rowlength - 1; rowindex++) {
currentrow = rows.get_row(rowindex);
currentAmount = currentrow.get_cellByColumnKey("Amount").get_value();
runningBalance += currentAmount;
currentrow.get_cellByColumnKey("Balance").set_value(runningBalance);
format_cellAmount(currentrow.get_cellByColumnKey("Balance"));
format_cellAmount(currentrow.get_cellByColumnKey("Amount"));
function format_cellAmount(dgcell) {
var currentvalue = dgcell.get_value();
if (currentvalue == 0) {
dgcell.set_text("-");
dgcell.get_element().className = "amount0";
if (currentvalue > 0) {
dgcell.get_element().className = "amountpositive";
if (currentvalue < 0) {
dgcell.get_element().className = "amountnegative";
</script>
*sigh*... could use a hand with this...
For some reason, when I bind this code to the initialize event, every postback triggers an UPDATE to every row in the grid.
I was careful to use set_text() instead of set_value()... thinking that that would prevent each row from being marked as "dirty". But evidently I was wrong.
Is there some way I can prevent the rows from being marked as dirty, while still adjusting the display?