美文网首页
resort and recalculate in a list

resort and recalculate in a list

作者: 超薄智能 | 来源:发表于2017-01-29 10:27 被阅读29次
    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

    相关文章

      网友评论

          本文标题:resort and recalculate in a list

          本文链接:https://www.haomeiwen.com/subject/fwccittx.html