📄 class1.cs
字号:
private string strCustomerID = "";
#region 控件事件代码
private void btnOrdersMovePrevious_Click(object sender, System.EventArgs e)
{
MovePrior();
}
private void btnExit_Click_1(object sender, System.EventArgs e)
{
if(dsOneToMany.HasChanges())
{
DialogResult result = MessageBox.Show("数据已被修改,是否保存修改?", "是否保存", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
if(result == DialogResult.Yes)
SubmitChanges();
else if(result == DialogResult.Cancel)
return;
}
this.Close();
}
private void btnSubmitChanges_Click(object sender, System.EventArgs e)
{
SubmitChanges();
}
private void btnDetailsDelete_Click(object sender, System.EventArgs e)
{
DeleteDetails();
}
private void btnDetailsAdd_Click(object sender, System.EventArgs e)
{
AddDetails();
}
private void grdDetails_Navigate(object sender, System.Windows.Forms.NavigateEventArgs ne)
{
}
private void btnDetailsEdit_Click(object sender, System.EventArgs e)
{
EditDetails();
}
private void btnOrdersCancel_Click(object sender, System.EventArgs e)
{
RevertOrders();
}
private void btnOrdersUpdate_Click(object sender, System.EventArgs e)
{
UpdateOrders();
}
private void btnOrdersDelete_Click(object sender, System.EventArgs e)
{
DeleteOrder();
}
private void btnOrdersAdd_Click(object sender, System.EventArgs e)
{
AddNewOrder();
}
private void btnOrdersEdit_Click_1(object sender, System.EventArgs e)
{
EditOrder();
}
private void btnOrdersMoveLast_Click(object sender, System.EventArgs e)
{
MoveLast();
}
private void lblOrdersPosition_Click(object sender, System.EventArgs e)
{
}
private void btnOrdersMoveNext_Click(object sender, System.EventArgs e)
{
MoveNext();
}
private void btnOrdersEdit_Click(object sender, System.EventArgs e)
{
EditOrder();
}
private void btnOrdersAdd_Click_1(object sender, System.EventArgs e)
{
AddNewOrder();
}
private void btnExit_Click(object sender, System.EventArgs e)
{
if(dsOneToMany.HasChanges())
{
DialogResult result = MessageBox.Show("数据已被修改,是否保存修改?", "是否保存", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
if(result == DialogResult.Yes)
SubmitChanges();
else if(result == DialogResult.Cancel)
return;
}
this.Close();
}
#endregion
#region 记录指针移动
private void MoveFirst()
{
if((vueOrders.Count > 0) && (BindingContext[vueOrders].Position != 0))
{
BindingContext[vueOrders].Position = 0;
int currentOrderID = (int)vueOrders[BindingContext[vueOrders].Position].Row["编号"];
int newOrdersPosition = 0;
while(dsOneToMany. 销售订单[newOrdersPosition]. 销售订单 != currentOrderID)
newOrdersPosition++;
BindingContext[dsOneToMany, "Orders"].Position = newOrdersPosition;
ShowCurrentOrder();
}
}
private void MovePrior()
{
if(BindingContext[vueOrders].Position > 0)
{
BindingContext[vueOrders].Position--;
int currentOrderID = (int)vueOrders[BindingContext[vueOrders].Position].Row["OrderID"];
int newOrdersPosition = BindingContext[dsOneToMany, " 销售订单"].Position;
while(dsOneToMany.销售订单[newOrdersPosition].OrderID != currentOrderID)
newOrdersPosition--;
BindingContext[dsOneToMany, " 销售订单"].Position = newOrdersPosition;
ShowCurrentOrder();
}
}
private void MoveNext()
{
if(vueOrders.Count > BindingContext[vueOrders].Position + 1)
{
BindingContext[vueOrders].Position++;
int currentOrderID = (int)vueOrders[BindingContext[vueOrders].Position].Row["订单编号"];
int newOrdersPosition = BindingContext[dsOneToMany, " 销售订单"].Position;
while(dsOneToMany. 销售订单[newOrdersPosition].订单编号 != currentOrderID)
newOrdersPosition ++;
BindingContext[dsOneToMany, "销售订单"].Position = newOrdersPosition;
ShowCurrentOrder();
}
}
private void MoveLast()
{
if(vueOrders.Count > BindingContext[vueOrders].Position + 1)
{
BindingContext[vueOrders].Position = vueOrders.Count - 1;
int currentOrderID = (int)vueOrders[BindingContext[vueOrders].Position].Row["订单编号"];
int newOrdersPosition = dsOneToMany.Orders.Count - 1;
while(dsOneToMany.销售订单[newOrdersPosition].订单编号 != currentOrderID)
newOrdersPosition--;
BindingContext[dsOneToMany, " 销售订单"].Position = newOrdersPosition;
ShowCurrentOrder();
}
}
private void ShowCurrentOrder()
{
lblOrdersPosition.Text = "订单 " + (BindingContext[vueOrders].Position + 1) + " 总数 " + vueOrders.Count;
if(vueOrders.Count != 0)
vueDetails.RowFilter = "OrderID = " + vueOrders[BindingContext[vueOrders].Position].Row["OrderID"];
}
#endregion
#region 编辑、添加、删除订单记录
private int oldVueOrdersPosition, oldOrdersPosition;
private bool addMode = false;
private void EditOrder()
{
if (vueOrders.Count > 0)
{
SetOrdersEditMode(true);
dsOneToMany.销售订单[this.BindingContext[dsOneToMany, "销售订单"].Position].BeginEdit();
}
else
MessageBox.Show("没有找到任何可以编辑的订单记录!", "订单编辑", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void AddNewOrder()
{
oldVueOrdersPosition = this.BindingContext[vueOrders].Position;
oldOrdersPosition = BindingContext[dsOneToMany, "销售订单"].Position;
addMode = true;
DSOneToMany.OrdersRow orderRow = dsOneToMany.销售订单.NewOrdersRow();
orderRow.客户编号 = strCustomerID;
orderRow.业务员号 = dsOneToMany.业务员号[0].业务员号;
orderRow.销售日期 = DateTime.Now;
dsOneToMany.销售订单.AddOrdersRow(orderRow);
BindingContext[dsOneToMany, "销售订单"].Position = dsOneToMany.销售订单.Count - 1;
BindingContext[vueOrders].Position = vueOrders.Count - 1;
dsOneToMany.销售订单[BindingContext[dsOneToMany, "销售订单"].Position].BeginEdit();
SetOrdersEditMode(true);
ShowCurrentOrder();
}
private void DeleteOrder()
{
if (vueOrders.Count > 0)
{
vueOrders.Delete(this.BindingContext[vueOrders].Position);
if (vueOrders.Count > 0)
{
if (BindingContext[vueOrders].Position == vueOrders.Count)
{
int currentOrderID = (int)vueOrders[BindingContext[vueOrders].Position].Row["销售订单"];
int newOrdersPosition = BindingContext[dsOneToMany, "销售订单"].Position;
while(dsOneToMany.销售订单[newOrdersPosition].编号 != currentOrderID)
newOrdersPosition--;
BindingContext[dsOneToMany, "销售订单"].Position = newOrdersPosition;
}
}
ShowCurrentOrder();
}
else
MessageBox.Show("没有可以删除的订单记录!", "删除订单", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void SetOrdersEditMode(bool blnEdit)
{
txtCustomerID.ReadOnly = !blnEdit;
txtOrderDate.ReadOnly = !blnEdit;
cboEmployee.Enabled = blnEdit;
btnOrdersMoveFirst.Enabled = !blnEdit;
btnOrdersMovePrevious.Enabled = !blnEdit;
btnOrdersMoveNext.Enabled = !blnEdit;
btnOrdersMoveLast.Enabled = !blnEdit;
btnOrdersCancel.Enabled = blnEdit;
btnOrdersUpdate.Enabled = blnEdit;
btnOrdersEdit.Enabled = !blnEdit;
btnOrdersAdd.Enabled = !blnEdit;
btnOrdersDelete.Enabled = !blnEdit;
btnSubmitChanges.Enabled = !blnEdit;
btnDetailsEdit.Enabled = !blnEdit;
btnDetailsAdd.Enabled = !blnEdit;
btnDetailsDelete.Enabled = !blnEdit;
}
#endregion
#region 保存、撤销对订单记录的修改
private void UpdateOrders()
{
BindingContext[dsOneToMany, "销售订单"].EndCurrentEdit();
if(addMode)
{
addMode = false;
this.BindingContext[vueOrders].Position = vueOrders.Count - 1;
}
SetOrdersEditMode(false);
ShowCurrentOrder();
}
private void RevertOrders()
{
BindingContext[dsOneToMany, "销售订单"].CancelCurrentEdit();
if(addMode)
{
addMode = false;
DeleteOrder();
this.BindingContext[vueOrders].Position = oldVueOrdersPosition;
BindingContext[dsOneToMany, "销售订单"].Position = oldOrdersPosition;
}
SetOrdersEditMode(false);
ShowCurrentOrder();
}
#endregion
#region 修改订单明细数据
private void EditDetails()
{
if (vueDetails.Count > 0)
{
frmEditDetail frmEditDetail = new frmEditDetail();
frmEditDetail.EditDetail(vueDetails[grdDetails.CurrentRowIndex]);
frmEditDetail.Dispose();
}
else
MessageBox.Show("没有当前订单!", "添加订单明细", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void AddDetails()
{
if (dsOneToMany.销售订单.Count == 0)
{
MessageBox.Show("没有当前订单!", "添加订单明细", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
DataRowView drvNewDetail = vueDetails.AddNew();
drvNewDetail["销售订单"] = dsOneToMany.销售订单[this.BindingContext[dsOneToMany, "销售订单"].Position].编号;
frmEditDetail frmEditDetail = new frmEditDetail();
frmEditDetail.EditDetail(drvNewDetail);
frmEditDetail.Dispose();
}
private void DeleteDetails()
{
if (vueDetails.Count > 0)
vueDetails[grdDetails.CurrentRowIndex].Delete();
else
MessageBox.Show("没有可以删除的订单明细项!", "订单明细删除", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
#endregion
#region 提交更新
private void SubmitChanges()
{
if (dsOneToMany.HasChanges())
{
conn.Open();
SqlTransaction txn = conn.BeginTransaction();
adpOrders.UpdateCommand.Transaction = txn;
adpOrders.InsertCommand.Transaction = txn;
adpOrders.DeleteCommand.Transaction = txn;
adpDetails.UpdateCommand.Transaction = txn;
adpDetails.InsertCommand.Transaction = txn;
adpDetails.DeleteCommand.Transaction = txn;
try
{
int intOrdersModified, intDetailsModified;
intOrdersModified = adpOrders.Update(dsOneToMany.销售订单.Select("", "", DataViewRowState.Added | DataViewRowState.ModifiedCurrent));
intDetailsModified = adpDetails.Update(dsOneToMany.销售订单明细);
intOrdersModified += adpOrders.Update(dsOneToMany.销售订单.Select("", "", DataViewRowState.Deleted));
txn.Commit();
MoveFirst();
string strOutput;
strOutput = "已修改 " + intOrdersModified + " 条订单信息\r\n" +
"已修改 " + intDetailsModified + " 条订单明细信息";
MessageBox.Show(strOutput, "更新成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "更新失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
txn.Rollback();
}
finally
{
conn.Close();
}
}
}
#endregion
#region 刷新数据
private void RefreshData()
{
dsOneToMany.Clear();
adpProducts.Fill(dsOneToMany.商品清单);
adpEmployees.Fill(dsOneToMany.业务员清单);
adpOrders.SelectCommand.Parameters[0].Value = strCustomerID;
adpOrders.Fill(dsOneToMany.销售订单);
adpDetails.SelectCommand.Parameters[0].Value = strCustomerID;
adpDetails.Fill(dsOneToMany.Order_Details);
ShowCurrentOrder();
SetOrdersEditMode(false);
}
#endregion
private void panel2_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
}
private void btnOrdersMoveFirst_Click(object sender, System.EventArgs e)
{
MoveFirst();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -