resort and recalculate in a list (C#)

resort and recalculate in a list (C#)_第1张图片
Screen Shot 2017-01-29 at 10.40.09 AM.png

aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DetailsPage.aspx.cs" Inherits="DetailsPage" %>





    


    
Item Code:
Balance in Inventory:

aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DetailsPage : System.Web.UI.Page
{
    string itemCode;
    StockCardTestingEntities sct;
    List tsList;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        //initialise
        sct = new StockCardTestingEntities();

        //get param and call method 
        //itemCode = Request.QueryString["itemCode"];
        itemCode = "C001";
        showDetails(itemCode);
        showStockCard(itemCode);
    }

    private void showStockCard(string itemCode)
    {
        

        //get transactions and sort by date 
        tsList = sct.Transactions.Where(x => x.ItemCode == itemCode).ToList();
        tsList = tsList.OrderByDescending(d => d.TranDate).ToList();

        //calculate Quantity in each row of the tsList
        //using InQty(the defaut attribute in the transaction model) to contain Quanity
        foreach (Transaction item in tsList)
        {
            if (String.IsNullOrEmpty(item.InQty)) //this is the proper way to check something whether is NULL or not
            {
                item.InQty = (Convert.ToInt32(item.OutQty) * -1).ToString(); // when InQty is NULL, means it must has OutQty, so at this case InQty = -1 * OutQty 
            }
        }

        //calculate Balance in each row of the tsList which is from above (Each InQty already be filled)
        //using OutQty to contain Balance

        //get current balance in inventory
        string currentBalance = sct.StockCards.Where(x => x.ItemCode == itemCode).Select(y => y.Balance).FirstOrDefault();
        txtBalance.Text = currentBalance;
        for (int i = 0; i< tsList.Count; i++)
        {
            // we only know the currrent balance from Stock Card Table
            if (i == 0) 
            {
                tsList[i].OutQty = currentBalance;
            }
            // other balance is based on the previous Balance(which is contained by OutQty ) minus previous Quantity(which is contained by InQty)
            //in other words: tsList[i].OutQty = tsList[i - 1].OutQty - Convert.ToInt32(tsList[i - 1].InQty
            else 
            {
                tsList[i].OutQty = (Convert.ToInt32(tsList[i - 1].OutQty)
                    - Convert.ToInt32(tsList[i - 1].InQty)).ToString();
            }
        }
        //Done!

        //show in the gridview
        GridView1.DataSource = tsList;
        GridView1.DataBind();
    }

    private void showDetails(string itemCode)
    {
        txtItemCode.Text = itemCode;
    }
}

Thinking Script

resort and recalculate in a list (C#)_第2张图片
IMG_20170129_104851.jpg

你可能感兴趣的:(resort and recalculate in a list (C#))