//新增科目
 private void btn_INSERTsub_Click(object sender, EventArgs e)
 {
     //實體化新增科目視窗並新增關閉表單事件
     Form_addSub f = new Form_addSub();
     f.FormClosed += new FormClosedEventHandler(addSub_over);
     f.ShowDialog(this);
 }
 /// <summary>
 /// 方法:編輯欄位資料
 /// </summary>
 /// <param name="cbo">科目名稱</param>
 /// <param name="nud">金額</param>
 /// <param name="txt_remark">備註</param>
 /// <param name="txt_total">顯示借貸平衡的區域</param>
 /// <param name="dt">目標資料表</param>
 /// <param name="dgv">顯示區域</param>
 /// <param name="s">0:新增,1:編輯,2:刪除</param>
 private void editRow(ComboBox cbo, NumericUpDown nud, TextBox txt_remark, TextBox txt_total, DataTable dt, DataGridView dgv, int s)
 {
     //新增或修改
     if (s == 0 || s == 1)
     {
         //判斷科目名稱是否存在
         int station = DataHandling.subexist(cbo.Text.ToString(), sublist);
         //不存在--不新增
         if (station == 0) return;
         //不存在--新增
         if (station == 2)
         {
             f = new Form_addSub(cbo.Text,0);
             f.FormClosed += F_FormClosed;
             f.ShowDialog(this);
             cbo.Text = f.subname;
             if (f.set == false) { MessageBox.Show("取消新增交易","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); return; } 
         }
         //判斷作用於借項或貸項
         if (cbo == cbo_Dsub) newTx = new object[7] { "000", dtp_Txdate.Value, txt_lot.Text, cbo.Text, "借", nud.Value, txt_remark.Text };
         else newTx = new object[7] { "000", dtp_Txdate.Value, txt_lot.Text, cbo.Text, "貸", nud.Value, txt_remark.Text };
         //判斷新增或修改
         if (s == 0) dt.Rows.Add(newTx);
         else if (s == 1)
         {
             if (dgv.SelectedRows.Count == 0) return;
             dt.Rows[dgv.SelectedRows[0].Index].ItemArray = newTx;
         }
     }
     //刪除
     else if (s == 2)
     {
         if (dgv.SelectedRows.Count == 0) return;
         dt.Rows[dgv.SelectedRows[0].Index].Delete();
     }
     //計算並顯示借方或貸方餘額
     object bal = dt.Compute("Sum(money)", "");
     txt_total.Text = bal.ToString();
     //清空欄位值
     cbo.Text = "";
     nud.Value = 1;
     txt_remark.Text = "";
     //取消選擇
     dgv.ClearSelection();
 }
 //新增或修改交易資料
 private void btn_edit_Click(object sender, EventArgs e)
 {
     //借方
     if (sender == btn_Dadd || sender == btn_Dupdate)
     {
         //判斷科目名稱是否存在
         int choice = DataHandling.subexist(cbo_Dsub.Text, sublist);
         if (choice == 0) return;
         else if (choice == 2)
         {
             f = new Form_addSub(cbo_Dsub.Text,0);
             f.FormClosed += Reload_Sublist;
             f.ShowDialog(this);
             cbo_Dsub.Text = f.subname;
         }
         //新增
         if (sender == btn_Dadd)
         {
             Txdata = new object[7] { "000", dtp_Txdate.Value, cbo_lot.Text, cbo_Dsub.Text, "借", nud_D.Value, txt_Dremark.Text };
             ds.Tables[1].Rows.Add(Txdata);
         }
         //修改
         else if (sender == btn_Dupdate)
         {
             object Tx_no = ds.Tables[1].Rows[dgv_D.SelectedRows[0].Index].ItemArray[0];
             Txdata = new object[7] { Tx_no, dtp_Txdate.Value, cbo_lot.Text, cbo_Dsub.Text, "借", nud_D.Value, txt_Dremark.Text };
             ds.Tables[1].Rows[dgv_D.SelectedRows[0].Index].ItemArray = Txdata;
         }
     }
     //貸方
     else if (sender == btn_Cadd || sender == btn_Cupdate)
     {
         //判斷科目名稱是否存在
         int choice = DataHandling.subexist(cbo_Csub.Text, sublist);
         if (choice == 0) return;
         else if (choice == 2)
         {
             f = new Form_addSub(cbo_Csub.Text,0);
             f.FormClosed += Reload_Sublist;
             f.ShowDialog(this);
             cbo_Csub.Text = f.subname;
         }
         //新增
         if (sender == btn_Cadd)
         {
             Txdata = new object[7] { "000", dtp_Txdate.Value, cbo_lot.Text, cbo_Csub.Text, "貸", nud_C.Value, txt_Cremark.Text};
             ds.Tables[2].Rows.Add(Txdata);
         }
         //修改
         else if (sender == btn_Cupdate)
         {
             object Tx_no = ds.Tables[2].Rows[dgv_C.SelectedRows[0].Index].ItemArray[0];
             Txdata = new object[7] { Tx_no, dtp_Txdate.Value, cbo_lot.Text, cbo_Csub.Text, "貸", nud_C.Value, txt_Cremark.Text};
             ds.Tables[2].Rows[dgv_C.SelectedRows[0].Index].ItemArray = Txdata;
         }
     }
 }