private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e) { try { //確認欄位型態 if (this.dataGridView2.Columns[e.ColumnIndex] is DataGridViewLinkColumn) { //確認欄位名稱 switch (dataGridView2.Columns[e.ColumnIndex].Name) { case "連接網頁": //觸發事件 //string keyword = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells["連接網頁"].Value); //string encKeyword = encKeyword = System.Web.HttpUtility.UrlEncode(keyword); string url = "https://stock.cnyes.com/market/TWS:TSE01:INDEX"; System.Diagnostics.Process.Start(url); break; } } if (this.dataGridView2.Columns[e.ColumnIndex] is DataGridViewButtonColumn) { switch (dataGridView2.Columns[e.ColumnIndex].Name) { case "交易": //觸發事件 FrmBuyStock frmBuyStock = new FrmBuyStock(); //Tag傳資料 frmBuyStock.Tag = dataGridView2.Rows[e.RowIndex]; //傳member資料過去 frmBuyStock.member = this.member; //傳frmhomepage資料過去 frmBuyStock.frmHomePage = this; frmBuyStock.ShowDialog(); break; case "刪除": //觸發事件 //先轉譯成可列舉 datasave result = db.datasave.AsEnumerable().Where(d => d.證券代號 == this.dataGridView2.Rows[e.RowIndex].Cells[4].Value.ToString() && d.account == member.account).FirstOrDefault(); dataModel.Delete(result); this.dataGridView2.DataSource = db.datasave.Where(m => m.account == member.account).ToList(); this.dataGridView2.Columns.Remove("id"); this.dataGridView2.Columns["account"].Visible = false; break; } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void btnOK_Click(object sender, EventArgs e) { try { members member = this.member; if (string.IsNullOrEmpty(this.txtNum.Text) || this.txtNum.Text == "0") { MessageBox.Show("請輸入賣出張數!!"); this.txtMoney.Text = "0"; return; } else if (int.Parse(this.txtNum.Text) > int.Parse(buttonRow.Cells[6].Value.ToString())) { MessageBox.Show("賣出張數超出庫存張數@@,請重新輸入!!"); this.txtNum.Text = ""; this.txtMoney.Text = "0"; return; } else { MessageBox.Show("賣出成功!!"); } //賣出張數,修改資料表 if (int.Parse(this.txtNum.Text) == int.Parse(buttonRow.Cells[6].Value.ToString())) { stocksave save = db.stocksave.AsEnumerable().Where(s => s.證券代號 == buttonRow.Cells[3].Value.ToString() && s.account == member.account).FirstOrDefault(); datamodel.Delete(save); stockremain remain = db.stockremain.AsEnumerable().Where(r => r.證券代號 == buttonRow.Cells[3].Value.ToString() && r.account == member.account).FirstOrDefault(); datamodel.Delete(remain); } else if (int.Parse(this.txtNum.Text) < int.Parse(buttonRow.Cells[6].Value.ToString())) { stocksave save = db.stocksave.AsEnumerable().Where(s => s.證券代號 == buttonRow.Cells[3].Value.ToString() && s.account == member.account).FirstOrDefault(); datamodel.Updatestocksave(save, int.Parse(this.txtNum.Text)); stockremain remainsave = db.stockremain.AsEnumerable().Where(r => r.證券代號 == buttonRow.Cells[3].Value.ToString() && r.account == member.account).FirstOrDefault(); datamodel.Updatestockremain(remainsave, int.Parse(this.txtNum.Text)); } double remainmoney = (double)member.bankmoney + double.Parse(txtMoney.Text); member.bankmoney = (int)remainmoney; var mem = (from m in db.members where m.account == member.account select m).FirstOrDefault(); if (mem == null) { return; } mem.bankmoney = member.bankmoney; db.SaveChanges(); //frmHomePage傳值 FrmHomePage frmHomePage = this.frmHomePage; frmHomePage.lblbankmoney.Text = member.bankmoney.ToString(); frmHomePage.lblmoney.Text = member.bankmoney.ToString(); frmHomePage.lblbank.Text = member.bankmoney.ToString(); //frmHomePage.dataGridView3.DataSource = null; //frmHomePage.dataGridView4.DataSource = null; frmHomePage.dataGridView4.DataSource = db.stockremain.Where(s => s.account == member.account).ToList(); frmHomePage.dataGridView4.Columns["id"].Visible = false; frmHomePage.dataGridView3.DataSource = db.stocksave.Where(s => s.account == member.account).ToList(); frmHomePage.dataGridView3.Columns["id"].Visible = false; this.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }