using System;
using System.ComponentModel;
using System.Data.Linq;
using System.Linq;
using System.Windows.Forms;
using ProductManager.Model;
namespace ProductManager
{
public partial class FormClient : Form
{
#region 自定义属性
private Client clientEntity;
private BindingList<Client> clientList;
private ProductSet dataBase;
#endregion
public FormClient()
{
#region
InitializeComponent();
maskedTextID.Mask = @"/C000"; // 自定义输入掩码, C 为特殊字符, 须转译。
splitContainer.SetStyle();
dgvClient.SetStyle();
dataBase = new ProductSet(DBConnection.SQL);
this.LoadData();
#endregion
}
#region LoadData
private void LoadData()
{
if (dataBase.DatabaseExists())
{
clientList = dataBase.Client.GetNewBindingList() as BindingList<Client>;
dgvClient.DataSource = clientList;
}
btnCancel_Click(null, EventArgs.Empty);
}
#endregion
#region AddRow
private void btnAdd_Click(object sender, EventArgs e)
{
var query = from item in clientList
let Name = item.ClientName.ToLowerInvariant()
let Value = txtName.Text.Trim().ToLowerInvariant()
where Name == Value
select item;
if (query.Any())
{
MessageBox.Show(string.Format("您输入的公司名称“{0}”已存在,请重新输入!", txtName.Text.Trim()), "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtName.Focus();
txtName.SelectAll();
return;
}
try
{
clientEntity = clientList.AddNew();
if (ValidateItem())
clientList.CancelNew(clientList.IndexOf(clientEntity));
else
{
dataBase.Client.InsertOnSubmit(clientEntity);
clientList.EndNew(clientList.IndexOf(clientEntity));
btnCancel_Click(null, EventArgs.Empty);
btnSave.Enabled = true;
}
}
catch
{
clientList.CancelNew(clientList.IndexOf(clientEntity));
MessageBox.Show(string.Format("客户编号“{0}”添加失败!", maskedTextID.Text), "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
#endregion
#region ValidateItem
public bool ValidateItem()
{
if (txtName.TextTrimLength() < 1)
{
MessageBox.Show("请输入的公司名称", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtName.Focus();
txtName.SelectAll();
}
else if (txtLinkman.TextTrimLength() < 1)
{
MessageBox.Show("请输入联系人名", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtLinkman.Focus();
txtLinkman.SelectAll();
}
else if (!maskedTextPhone.IsValidPhone())
{
MessageBox.Show("联系电话号码格式不正确,请输入联系电话号码", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
maskedTextPhone.Focus();
maskedTextPhone.SelectAll();
}
else if (!maskedTextFax.IsValidPhone())
{
MessageBox.Show("传真号码格式不正确,请输入传真号码", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
maskedTextFax.Focus();
maskedTextFax.SelectAll();
}
else if (!txtEmail.IsValidEmail())
{
MessageBox.Show("电子邮件格式不正确,请输入电子邮件", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtEmail.Focus();
txtEmail.SelectAll();
}
else
{
clientEntity.ClientID = maskedTextID.Text;
clientEntity.ClientName = txtName.Text.Trim();
clientEntity.Linkman = txtLinkman.Text.Trim();
clientEntity.LinkPhone = maskedTextPhone.Text;
clientEntity.Fax = maskedTextFax.Text;
clientEntity.Email = txtEmail.Text.Trim();
clientEntity.Address = txtAddress.Text.Trim();
clientEntity.Memo = txtMemo.Text;
return false;
}
return true;
}
#endregion
#region UpdateRow
private void btnModify_Click(object sender, EventArgs e)
{
var query = from item in clientList
let Name = item.ClientName.ToLowerInvariant()
let Value = txtName.Text.Trim().ToLowerInvariant()
let Current = clientEntity.ClientName.ToLowerInvariant()
where Name == Value && Name != Current
select item;
if (query.Any())
{
MessageBox.Show(string.Format("您输入的公司名称“{0}”已存在,请重新输入!", txtName.Text.Trim()), "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtName.Focus();
txtName.SelectAll();
return;
}
try
{
if (ValidateItem())
return;
btnCancel_Click(null, EventArgs.Empty);
btnSave.Enabled = true;
}
catch
{
MessageBox.Show(string.Format("客户编号“{0}”修改失败!", maskedTextID.Text), "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
#endregion
#region DeleteRow
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
if (MessageBox.Show("真的要删除此记录吗?", "确定删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question).Equals(DialogResult.OK))
{
dataBase.Client.DeleteOnSubmit(clientEntity);
clientList.Remove(clientEntity);
if (clientEntity.Equals(dgvClient.CurrentRow.DataBoundItem as Client))
(dgvClient.DataSource as BindingList<Client>).Remove(clientEntity);
btnCancel_Click(null, EventArgs.Empty);
btnSave.Enabled = true;
}
}
catch
{
MessageBox.Show(string.Format("客户编号“{0}”删除失败!", maskedTextID.Text), "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
#endregion
#region SaveData
private void btnSave_Click(object sender, EventArgs e)
{
try
{
dataBase.SubmitChanges(ConflictMode.ContinueOnConflict); // 指定应尝试对数据库的所有更新,并且应在该过程结束时累积和返回并发冲突。
}
catch (ChangeConflictException ce)
{
foreach (ObjectChangeConflict occ in dataBase.ChangeConflicts)
{
occ.Resolve(RefreshMode.OverwriteCurrentValues); // 通过保留数据库值解决并发冲突。
}
ce.Source = null;
}
catch
{
MessageBox.Show("客户信息保存失败,将重新加载所有客户信息!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
finally
{
btnSave.Enabled = false;
this.LoadData();
}
}
#endregion
#region CancelButton
private void btnCancel_Click(object sender, EventArgs e)
{
gbText.ClearText();
this.SetEnabled(true);
GetCurrentPage();
var query = from item in clientList
let ClientID = item.ClientID.Substring(1)
select ClientID;
int id = query.Any() ? int.Parse(query.Max()) + 1 : 1;
maskedTextID.Text = string.Format("C{0:D3}", id);
txtName.Focus();
}
#endregion
#region FindRows
private void btnQuery_Click(object sender, EventArgs e)
{
var query = from item in clientList // 自动从数据库更新查询
let ClientName = item.ClientName.Contains(txtName.Text.Trim())
let Linkman = item.Linkman.Contains(txtLinkman.Text.Trim())
where ClientName && Linkman
select item;
if (query.Any())
dgvClient.DataSource = new BindingList<Client>(query.ToList());
else
MessageBox.Show("没有您要的信息,请重新输入!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
btnCancel_Click(null, EventArgs.Empty);
}
#endregion
#region MaskInputRejected
private void maskedTextPhone_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
{
e.ShowMessage(maskedTextPhone, "请输入电话号码!");
}
private void maskedTextFax_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
{
e.ShowMessage(maskedTextFax, "请输入传真号码!");
}
#endregion
#region GetDataBoundItem
private void dgvClient_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0)
return;
clientEntity = dgvClient.CurrentRow.DataBoundItem as Client;
maskedTextID.Text = clientEntity.ClientID;
txtName.Text = clientEntity.ClientName;
txtLinkman.Text = clientEntity.Linkman;
maskedTextPhone.Text = clientEntity.LinkPhone;
maskedTextFax.Text = clientEntity.Fax;
txtEmail.Text = clientEntity.Email;
txtAddress.Text = clientEntity.Address;
txtMemo.Text = clientEntity.Memo;
dgvClient.TopLeftHeaderCell.Value = string.Format("{0:#,##0}", e.RowIndex + 1);
this.GetCurrentPage();
this.SetEnabled(false);
}
#endregion
#region SetEnabled
private void SetEnabled(bool flag)
{
btnAdd.Enabled = flag;
btnModify.Enabled = !flag;
btnDelete.Enabled = !flag;
}
#endregion
#region 分页导航
/// <summary>
/// 获取当前所在页。
/// </summary>
private void GetCurrentPage()
{
int rowIndex = dgvClient.FirstDisplayedScrollingRowIndex;
int size = dgvClient.DisplayedRowCount(false);
if (size < 1)
size = 1;
firstButton.Enabled = previousButton.Enabled = (rowIndex > 0);
lastButton.Enabled = nextButton.Enabled = (rowIndex + size < dgvClient.RowCount);
labelPosition.Text = string.Format("共 {0} 页", (dgvClient.RowCount + size - 1) / size);
}
private void dgvClient_Resize(object sender, EventArgs e)
{
GetCurrentPage();
}
private void PositionPage_Click(object sender, System.EventArgs e)
{
int size = dgvClient.DisplayedRowCount(false);
switch ((sender as Button).Name)
{
case "firstButton":
dgvClient.FirstDisplayedScrollingRowIndex = 0;
break;
case "previousButton":
if (dgvClient.FirstDisplayedScrollingRowIndex > size)
dgvClient.FirstDisplayedScrollingRowIndex -= size;
else
goto case "firstButton";
break;
case "nextButton":
if (dgvClient.FirstDisplayedScrollingRowIndex + size < dgvClient.RowCount)
dgvClient.FirstDisplayedScrollingRowIndex += size;
else
goto case "lastButton";
break;
case "lastButton":
dgvClient.FirstDisplayedScrollingRowIndex = dgvClient.RowCount - size;
break;
}
GetCurrentPage();
}
#endregion
#region FormClosing
private void FormClient_FormClosing(object sender, FormClosingEventArgs e)
{
if (btnSave.Enabled)
{
switch (MessageBox.Show("是否保存对“客户信息”的更改?", "系统询问", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation))
{
case DialogResult.Yes:
btnSave.PerformClick();
break;
case DialogResult.No:
break;
case DialogResult.Cancel:
e.Cancel = true;
break;
}
}
this.Tag = e.Cancel;
}
#endregion
}
}