/// <summary>
        /// 添加事物
        /// </summary>
        /// <param name="key"></param>
        /// <param name="transaction"></param>
        public static void AddTransaction(string key, MyTransaction transaction)
        {
            var obj = CallContext.LogicalGetData(CONTEXT_TRANSACTIONS_COLLECTION);

            List <MyTransaction> MyTransactions = null;

            if (obj == null)
            {
                MyTransactions = new List <MyTransaction>();
                MyTransactions.Add(transaction);
            }
            else
            {
                if (obj is List <MyTransaction> )
                {
                    MyTransactions = obj as List <MyTransaction>;
                    if (MyTransactions.SingleOrDefault(t => t.DataBase == key) == null)
                    {
                        MyTransactions.Add(transaction);
                    }
                }
                else
                {
                    throw new InvalidCastException("字典转换失败");
                }
            }

            CallContext.LogicalSetData(CONTEXT_TRANSACTIONS_COLLECTION, MyTransactions);
        }
        private void CalculateCashBalance()
        {
            // This does not change MyTransactions. You could add .ToList() but I don't think that will help me.
            MyTransactions.OrderBy(t => t.TheTransaction.Date);

            // Update the running total for each transaction.
            ListCollectionView blah = (ListCollectionView)CollectionViewSource.GetDefaultView(MyTransactions);

            blah.CustomSort = new SortTransactionVmByDate();
            decimal balance = 0.0M;

            foreach (Transaction_VM xact in blah)
            {
                switch (xact.TheTransaction.Type)
                {
                case (int)ModelService.TransactionType.Deposit:
                case (int)ModelService.TransactionType.Sell:
                case (int)ModelService.TransactionType.Dividend:
                    balance += xact.TheTransaction.Value;
                    break;

                case (int)ModelService.TransactionType.Withdrawal:
                case (int)ModelService.TransactionType.Buy:
                case (int)ModelService.TransactionType.Fee:
                    balance -= xact.TheTransaction.Value;
                    break;
                }
                xact.CashBalance = balance;
            }

            // Update the Account value.
            RaisePropertyChanged("TheAccount");
        }
        private void AddTransaction()
        {
            // Create the object and update the database.
            Transaction t = new Transaction();

            ModelService.AddTransaction(TheAccount, t);

            // Create the VM object and update the app/UI.
            Transaction_VM tvm = new Transaction_VM();

            tvm.TheTransaction = t;
            MyTransactions.Add(tvm);

            MyParent.NotifyNumXacts();
        }
        public void DeleteTransaction()
        {
            if (SelectedTransaction != null)
            {
                ModelService.DeleteTransaction(SelectedTransaction.TheTransaction);
                MyTransactions.Remove(SelectedTransaction);
                // TODO: Move the selection to the next/previous item in the list.
                SelectedTransaction = null;

                CalculateCashBalance();
                PopulatePositions();
                MyParent.NotifyNumXacts();
                RaisePropertyChanged("Value");
                RaisePropertyChanged("TheAccount");
            }
        }
        private void BindGrid()
        {
            OleDbConnection myConnection = new OleDbConnection();

            myConnection.ConnectionString = ConfigurationManager.ConnectionStrings["DBConnStr"].ConnectionString;

            string sAfter  = Request.Form["after"];
            string sBefore = Request.Form["before"];

            string sSQL = "SELECT t.transid, t.accountid, t.description, t.amount FROM transactions t INNER JOIN accounts a ON t.accountid = a.accountid where 1=1 ";

            if (!(sAfter == null || sAfter == ""))
            {
                sSQL += " and t.trans_date >= " + sAfter;
            }
            if (!(sBefore == null || sBefore == ""))
            {
                sSQL += " and t.trans_date <= " + sBefore;
            }
            sSQL += " and a.userid = " + Request.Cookies["amUserId"].Value + " ORDER BY 1 DESC";

            // Format the dates for the respective databases
            string sDateRegEx = "((0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.]([0-9]{4}))";

            if (ConfigurationManager.ConnectionStrings["DBConnStr"].ConnectionString.Contains("Microsoft.Jet.OLEDB.4.0"))
            {
                sSQL = Regex.Replace(sSQL, sDateRegEx, "#$4-$3-$2#");

                // Hack for MS Access which can not terminate a string
                sSQL = Regex.Replace(sSQL, "--.*", "");
            }
            else
            {
                sSQL = Regex.Replace(sSQL, sDateRegEx, "'$4-$3-$2'");
            }

            myTransactions = new OleDbDataAdapter(sSQL, myConnection);
            DataSet ds = new DataSet();

            myTransactions.Fill(ds, "trans");
            DataView Source = ds.Tables["trans"].DefaultView;

            MyTransactions.DataSource = Source;
            MyTransactions.DataBind();
            DisableViewState(MyTransactions);
        }