让知识更加联贯 让技术走进生活
我的博客
我的程序 我的网络
------郑紫至
E-mail:zhengzizhi@yahoo.com.cn
地磅称量系统
56
. 从ToolBox中拖放一个ErrorProvider控件放到FrmWtBill界面上并将它的DataSource
属性设置成bindingSource1 接下来就是写代码了添加一个DisplayErrors()方法
然后改一下button1Save_Click内的代码就OK啦,完整代码如下所示:
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
LWSMapping;
using
System.Linq;
namespace
WinApp
{
public partial class FrmWtBill : BaseForm.MyTemplateForm
{
public FrmWtBill()
{
InitializeComponent();
}
private WtBillMappingDataContext db;
private void FrmWtBill_Load(object sender, EventArgs e)
{
db = new WtBillMappingDataContext();
var WtBills = from a in db.WtBills
orderby a.BillNo
select a;
BindData();
this.bindingSource1.DataSource = WtBills;
//InitColumn()
放在提供数据源控件之后初始化这样每列次序会按照我们预期的顺序排列。
//
不使用编程方式来实现每列的属性也可以,在dataGridView1上直接编辑每列的属性。
InitColumn();
}
///<summary>
///
将提供数据源的控件绑定到用户用来编辑数据的控件上
///</summary>
private void BindData()
{
this.txtBillNo.DataBindings.Add("Text", this.bindingSource1, "BillNo");
this.cmbVehicleNo.DataBindings.Add("Text", this.bindingSource1, "VehicleNo");
this.txtGrossTime.DataBindings.Add("Text", this.bindingSource1, "GrossTime");
this.txtTraceTime.DataBindings.Add("Text", this.bindingSource1, "TraceTime");
this.cmbCargoName.DataBindings.Add("Text", this.bindingSource1, "CargoName");
this.txtGrossWt.DataBindings.Add("Text", this.bindingSource1, "GrossWt");
this.txtTraceWt.DataBindings.Add("Text", this.bindingSource1, "TraceWt");
this.txtNetWt.DataBindings.Add("Text", this.bindingSource1, "NetWt");
this.cmbCustomerName.DataBindings.Add("Text", this.bindingSource1, "CustomerName");
this.txtOperator.DataBindings.Add("Text", this.bindingSource1, "Operator");
}
///<summary>
///
使用编程的方式用来控制dataGridView控件的每列属性
///</summary>
private void InitColumn()
{
this.dataGridView1.Columns.Clear();
System.Windows.Forms.DataGridViewTextBoxColumn txtBoxColumn = null;
System.Windows.Forms.DataGridViewCheckBoxColumn checkBoxColumn = null;
checkBoxColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn();
checkBoxColumn.DataPropertyName = "checkBox";
checkBoxColumn.Name = "checkBox";
checkBoxColumn.HeaderText = "
打印"
;
checkBoxColumn.Width = 35;
checkBoxColumn.ReadOnly = false;
checkBoxColumn.DisplayIndex = 0;
this.dataGridView1.Columns.Add(checkBoxColumn);
txtBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
txtBoxColumn.DataPropertyName = "BillNo";
txtBoxColumn.Name = "BillNo";
txtBoxColumn.HeaderText = "
单据编号"
;
txtBoxColumn.Width = 90;
txtBoxColumn.ReadOnly = true;
txtBoxColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
txtBoxColumn.DisplayIndex = 1;
this.dataGridView1.Columns.Add(txtBoxColumn);
txtBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
txtBoxColumn.DataPropertyName = "VehicleNo";
txtBoxColumn.Name = "VehicleNo";
txtBoxColumn.HeaderText = "
车牌编号"
;
txtBoxColumn.Width = 70;
txtBoxColumn.ReadOnly = true;
txtBoxColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
txtBoxColumn.DisplayIndex = 2;
this.dataGridView1.Columns.Add(txtBoxColumn);
txtBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
txtBoxColumn.DataPropertyName = "GrossTime";
txtBoxColumn.Name = "GrossTime";
txtBoxColumn.HeaderText = "
毛称时间"
;
txtBoxColumn.Width = 110;
txtBoxColumn.ReadOnly = true;
txtBoxColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
txtBoxColumn.DisplayIndex = 3;
this.dataGridView1.Columns.Add(txtBoxColumn);
txtBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
txtBoxColumn.DataPropertyName = "TraceTime";
txtBoxColumn.Name = "TraceTime";
txtBoxColumn.HeaderText = "
皮称时间"
;
txtBoxColumn.Width = 110;
txtBoxColumn.ReadOnly = true;
txtBoxColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
txtBoxColumn.DisplayIndex = 4;
this.dataGridView1.Columns.Add(txtBoxColumn);
txtBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
txtBoxColumn.DataPropertyName = "CargoName";
txtBoxColumn.Name = "CargoName";
txtBoxColumn.HeaderText = "
货物名称"
;
txtBoxColumn.Width = 70;
txtBoxColumn.ReadOnly = true;
txtBoxColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
txtBoxColumn.DisplayIndex = 5;
this.dataGridView1.Columns.Add(txtBoxColumn);
txtBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
txtBoxColumn.DataPropertyName = "GrossWt";
txtBoxColumn.Name = "GrossWt";
txtBoxColumn.HeaderText = "
毛重"
;
txtBoxColumn.Width = 70;
txtBoxColumn.ReadOnly = true;
txtBoxColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
txtBoxColumn.DisplayIndex = 6;
this.dataGridView1.Columns.Add(txtBoxColumn);
txtBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
txtBoxColumn.DataPropertyName = "TraceWt";
txtBoxColumn.Name = "TraceWt";
txtBoxColumn.HeaderText = "
皮重"
;
txtBoxColumn.Width = 70;
txtBoxColumn.ReadOnly = true;
txtBoxColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
txtBoxColumn.DisplayIndex = 7;
this.dataGridView1.Columns.Add(txtBoxColumn);
txtBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
txtBoxColumn.DataPropertyName = "NetWt";
txtBoxColumn.Name = "NetWt";
txtBoxColumn.HeaderText = "
净重"
;
txtBoxColumn.Width = 70;
txtBoxColumn.ReadOnly = true;
txtBoxColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
txtBoxColumn.DisplayIndex = 8;
this.dataGridView1.Columns.Add(txtBoxColumn);
txtBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
txtBoxColumn.DataPropertyName = "Unit";
txtBoxColumn.Name = "Unit";
txtBoxColumn.HeaderText = "
单位"
;
txtBoxColumn.Width = 35;
txtBoxColumn.ReadOnly = true;
txtBoxColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
txtBoxColumn.DisplayIndex = 9;
this.dataGridView1.Columns.Add(txtBoxColumn);
txtBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
txtBoxColumn.DataPropertyName = "CustomerName";
txtBoxColumn.Name = "CustomerName";
txtBoxColumn.HeaderText = "
客户名称"
;
txtBoxColumn.Width = 120;
txtBoxColumn.ReadOnly = true;
txtBoxColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
txtBoxColumn.DisplayIndex = 10;
this.dataGridView1.Columns.Add(txtBoxColumn);
txtBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
txtBoxColumn.DataPropertyName = "Operator";
txtBoxColumn.Name = "Operator";
txtBoxColumn.HeaderText = "
司称人员"
;
txtBoxColumn.Width = 70;
txtBoxColumn.ReadOnly = true;
txtBoxColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
txtBoxColumn.DisplayIndex = 11;
this.dataGridView1.Columns.Add(txtBoxColumn);
}
private void button1New_Click(object sender, EventArgs e)
{
this.bindingSource1.AddNew();
}
private void button1Modify_Click(object sender, EventArgs e)
{
//
修改数据的代码不需要实现
}
private void button1Delete_Click(object sender, EventArgs e)
{
if (DeleteConfirmMsg("
您真的要删除这条数据吗?"
, "
确认信息"
))
{
this.bindingSource1.RemoveCurrent();
button1Save_Click(null, null);
}
}
private void button1Query_Click(object sender, EventArgs e)
{
}
private void button1Print_Click(object sender, EventArgs e)
{
}
//
保存按钮界面上看不到,可以在属性下拉列表中找到它,双击button1Save的Click事件生成
private void button1Save_Click(object sender, EventArgs e)
{
this.Validate();
this.bindingSource1.EndEdit();
try
{
db.SubmitChanges();
MessageBox.Show("
您的数据已经成功更新"
);
}
catch (ValidationException ex)
{
this.DisplayErrors();
MessageBox.Show("
在数据提交保存之前请更正出现的错误"
);
}
}
//
取消按钮界面上看不到,可以在属性下拉列表中找到它,双击button1Cancel的Click事件生成
private void button1Cancel_Click(object sender, EventArgs e)
{
this.bindingSource1.CancelEdit();
}
private void DisplayErrors()
{
this.errorProvider1.UpdateBinding();
this.dataGridView1.Refresh();
if (this.bindingSource1.Position > -1)
{
WtBill currentWtBill = (WtBill)(bindingSource1.Current);
if (!currentWtBill.HasErrors)
{
for (int i = 0; i < this.bindingSource1.Count - 1; i++)
{
WtBill _WtBill = (WtBill)(bindingSource1[i]);
if (_WtBill.HasErrors)
{
this.bindingSource1.Position = i;
break;
}
}
}
}
}
}
}