/// <summary>
 /// 更新保存单据的表头和内容
 /// </summary>
 /// <param name="e">待更新的数据内容</param>
 private bool UpdateBill(FormViewUpdateEventArgs e)
 {
     //当前单据号
     string billNum = e.Keys["bill_num"].ToString();
     //实例化数据适配器
     using (var daHead = new t_delivery_bill_headTableAdapter())
     using (var daContent = new t_delivery_bill_contentTableAdapter())
     using (var daLotCard = new t_delivery_bill_lot_cardTableAdapter())
     //取得数据库连接
     using (var conn = daHead.Connection)
     {
         //打开数据库连接
         conn.Open();
         //事务
         using (var tran = conn.BeginTransaction())
         {
             //设置数据库连接
             daHead.Connection = daContent.Connection = daLotCard.Connection = conn;
             //设置事务
             daHead.Transaction = daContent.Transaction = daLotCard.Transaction = tran;
             try
             {
                 //记录需要检测更新首次送货记录工具费的订单序号清单
                 var lOrderId = new List<string>();
                 //取得之前单据清单记录
                 var tabContent = daContent.GetDataByBillNum(billNum);
                 //遍历各行
                 foreach (DataSetDeliveryBillMgr.t_delivery_bill_contentRow row in tabContent.Rows)
                 {
                     //执行更新订单已交货数和未交货数
                     daContent.UpdateOrderBalanceQty(-row.pcs_qty, row.order_id);
                     //之前是否有设置为需要记录工具费
                     if (!row.Isneed_tool_amountNull() && row.need_tool_amount)
                     {
                         //记录到清单
                         if (!lOrderId.Contains(row.order_id))
                         {
                             lOrderId.Add(row.order_id);
                         }
                     }
                 }
                 //先执行删除原来单据
                 daHead.Delete(billNum);
                 daContent.DeleteByBillNum(billNum);
                 daLotCard.DeleteByBillNum(billNum);
                 //保存表头内容
                 daHead.Insert(
                     Convert.ToDateTime(e.NewValues["bill_date"]),
                     Convert.ToString(e.NewValues["cust_name"]),
                     billNum,
                     Convert.ToString(e.NewValues["remark"]),
                     Convert.ToString(e.NewValues["add_person"]),
                     Convert.ToDateTime(e.NewValues["add_time"]),
                     Convert.ToDateTime(e.NewValues["last_change_time"])
                 );
                 //循环保存表身内容
                 for (int i = 0; i < 6; i++)
                 {
                     //当前行号
                     byte rowId = Convert.ToByte(i + 1);
                     //取得之前送货单当前行内容
                     var tab = daContent.GetDataByBillNumAndRowId(billNum, rowId);
                     //检测是否存在数据
                     if (tab.Rows.Count > 0)
                     {
                         var row = (DataSetDeliveryBillMgr.t_delivery_bill_contentRow)tab.Rows[0];
                         //取得之前写入的批量卡清单
                         string strLotCardListOld = row.lot_id_list.Trim();
                         if (strLotCardListOld.Length > 0)
                         {
                             //取得之前写入的批量卡清单
                             string productNumOld = row.product_num.Trim();
                             //之前的批量卡清单写回
                             LotCardListToWaitInStoreBalance(tran, billNum, rowId, strLotCardListOld, productNumOld);
                             //删除该条单据清单行数据
                             daContent.DeleteByBillNumAndRowId(billNum, rowId);
                         }
                     }
                     //当前数据所在的行
                     var trContentRow = (TableRow)tabDataListSon.FindControl("trContentRow" + i);
                     //获取该行生产编号
                     var txtProductNum = (TextBox)trContentRow.FindControl("txtProductNum" + i);
                     //检测生产编号存在就保存该行
                     if (txtProductNum == null)
                     {
                         throw new Exception("在第 " + rowId.ToString() + " 行未找到生产编号文本框!");
                     }
                     //交货数量文本框
                     var txtPcsQty = (TextBox)trContentRow.FindControl("txtPcsQty" + i);
                     int iPcsQty;
                     if (!int.TryParse(txtPcsQty.Text, out iPcsQty))
                     {
                         iPcsQty = 0;
                     }
                     var txtFocPcsQty = (TextBox)trContentRow.FindControl("txtFocPcsQty" + i);
                     int iFocPcsQty;
                     if (!int.TryParse(txtFocPcsQty.Text.Trim(), out iFocPcsQty))
                     {
                         iFocPcsQty = 0;
                     }
                     if (iPcsQty + iFocPcsQty <= 0)
                     {
                         continue;
                     }
                     //保存生产编号到变量
                     string productNum = txtProductNum.Text;
                     //输入的生产编号
                     if (productNum.Length <= 0)
                     {
                         throw new Exception("第 " + rowId.ToString() + " 行的生产编号文本框未输入内容!");
                     }
                     //其他各参数
                     var txtOrderId = (TextBox)trContentRow.FindControl("txtOrderId" + i);
                     var txtCustNum = (TextBox)trContentRow.FindControl("txtCustNum" + i);
                     var txtOrderNum = (TextBox)trContentRow.FindControl("txtOrderNum" + i);
                     //当前批量卡清单内容保存到的隐藏对象
                     var hdLotCardList = (HtmlInputHidden)tabDataListSon.FindControl("hdLotCardList" + i);
                     string strLotCardList = hdLotCardList.Value.Replace("|l|", "<").Replace("|g|", ">");
                     var txtRemark = (TextBox)trContentRow.FindControl("txtRemark" + i);
                     //订单序号
                     var orderId = txtOrderId.Text.Trim();
                     //执行扣减待入仓结存并将记录写入批量卡清单
                     DeductionWaitInStoreBalance(
                         tran,
                         strLotCardList,
                         billNum,
                         rowId,
                         productNum
                     );
                     //执行插入行
                     daContent.Insert(
                         billNum,
                         rowId,
                         orderId,
                         null,
                         productNum,
                         Convert.ToString(txtCustNum.Text),
                         txtOrderNum.Text,
                         null,
                         null,
                         iPcsQty,
                         iFocPcsQty == 0 ? null : (int?)iFocPcsQty,
                         null,
                         Convert.ToString(txtRemark.Text),
                         strLotCardList,
                         false
                     );
                     //执行更新订单已交货数和未交货数
                     daContent.UpdateOrderBalanceQty(iPcsQty, orderId);
                     //记录到清单
                     if (!lOrderId.Contains(orderId))
                     {
                         lOrderId.Add(orderId);
                     }
                 }
                 //更新需要显示工具费的行
                 UpdateNeedToolAmount(daContent, lOrderId);
                 //提交事务
                 tran.Commit();
                 //返回成功
                 return true;
             }
             catch (Exception ex)
             {
                 //回滚事务
                 tran.Rollback();
                 //抛出错误
                 throw ex;
             }
         }
     }
 }
 /// <summary>
 /// 根据单号从数据库中删除单据的表头和内容
 /// </summary>
 /// <param name="billNum">单据号</param>
 private bool DeleteBillByBillNum(string billNum)
 {
     //实例化数据适配器
     using (var daHead = new t_delivery_bill_headTableAdapter())
     using (var daContent = new t_delivery_bill_contentTableAdapter())
     using (var daLotCard = new t_delivery_bill_lot_cardTableAdapter())
     //取得数据库连接
     using (var conn = daHead.Connection)
     {
         //打开数据库连接
         conn.Open();
         //事务
         using (var tran = conn.BeginTransaction())
         {
             //设置数据库连接
             daHead.Connection = daContent.Connection = daLotCard.Connection = conn;
             //设置事务
             daHead.Transaction = daContent.Transaction = daLotCard.Transaction = tran;
             try
             {
                 //记录需要检测更新首次送货记录工具费的订单序号清单
                 var lOrderId = new List<string>();
                 //取得之前单据清单记录
                 var tabContent = daContent.GetDataByBillNum(billNum);
                 //遍历各行
                 foreach (DataSetDeliveryBillMgr.t_delivery_bill_contentRow row in tabContent.Rows)
                 {
                     //之前是否有设置为需要记录工具费
                     if (!row.Isneed_tool_amountNull() && row.need_tool_amount)
                     {
                         //记录到清单
                         if (!lOrderId.Contains(row.order_id))
                         {
                             lOrderId.Add(row.order_id);
                         }
                     }
                     //取得之前写入的批量卡清单
                     string strLotCardListOld = row.lot_id_list.Trim();
                     //是否存在批量卡清单
                     if (strLotCardListOld.Length > 0)
                     {
                         //当前生产编号
                         string productNum = row.product_num;
                         //之前的清单写回
                         LotCardListToWaitInStoreBalance(tran, billNum, null, strLotCardListOld, productNum);
                     }
                     //当前交货的数量
                     var pcsQty = row.pcs_qty;
                     if (!row.Isfoc_pcs_qtyNull())
                     {
                         pcsQty += row.foc_pcs_qty;
                     }
                     //执行更新订单已交货数和未交货数
                     daContent.UpdateOrderBalanceQty(-pcsQty, row.order_id);
                 }
                 //执行删除
                 daHead.Delete(billNum);
                 daContent.DeleteByBillNum(billNum);
                 daLotCard.DeleteByBillNum(billNum);
                 //更新需要显示工具费的行
                 UpdateNeedToolAmount(daContent, lOrderId);
                 //提交事务
                 tran.Commit();
                 //返回成功
                 return true;
             }
             catch (Exception ex)
             {
                 //回滚事务
                 tran.Rollback();
                 //抛出错误
                 throw ex;
             }
         }
     }
 }