resort and recalculate in a list (C#)

Screen Shot 2017-01-29 at 10.40.09 AM.png

aspx

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Panel ID="Panel1" runat="server" GroupingText="Details">
            Item Code: <asp:TextBox ID="txtItemCode" runat="server"></asp:TextBox>
        </asp:Panel>
        <br />
        <asp:Panel ID="Panel2" runat="server" GroupingText="Stock Card">
            Balance in Inventory: <asp:TextBox ID="txtBalance" runat="server"></asp:TextBox>

            <br />
            <br />
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundField DataField="TranDate" HeaderText="Date" />
                    <asp:BoundField DataField="Description" HeaderText="Description" />
                    <asp:BoundField DataField="InQty" HeaderText="Quantity" />
                    <asp:BoundField DataField="OutQty" HeaderText="Balance" />
                </Columns>
            </asp:GridView>

        </asp:Panel>
    </div>
    </form>
</body>
</html>

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<Transaction> 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

IMG_20170129_104851.jpg
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 不知不觉已经深秋了,黄色逐渐成了这个季节的主旋律,路上行人脚步匆匆向前方走去,黄色树叶随着过往的汽车来回翻滚,四散...
    冷风过境_007阅读 270评论 0 2
  • ios 9报警告如下: CGContextSaveGState: invalid context 0x0. If ...
    Z哥阅读 6,463评论 3 51
  • 无事闲来探浮生, 为求彻悟淡名利。 前尘往事为哪般? 劝君莫负眼前人。
    夏如故阅读 349评论 0 0
  • 一只美丽娇小的蜻蜓 连续二次早餐时 不知她来自何方 悄然地停落在我的手背上 第一天 我以为是偶然 第二天 又欣然而...
    一叶流芳阅读 297评论 2 3
  • 当我们用一种世俗的眼光去看待问题时,最终只能得到一个世俗的答案
    钟那个谁阅读 148评论 0 0