让知识更加联贯 让技术走进生活
我的博客
我的程序 我的网络
------郑紫至
E-mail:zhengzizhi@yahoo.com.cn
地磅称量系统
37.直接通过SQL 向数据库里面名称为WtBill的表添加两条测试数据
INSERT
INTO WtBill
(BillNo,VehicleNo,GrossTime,TraceTime,CargoName
,GrossWt,TraceWt,NetWt,Unit,CustomerName,Operator)
VALUES
('20080302-0001' ,N'
粤
B12345'
,'2008-3-2 10:30:00' ,'2008-3-2 14:36:50'
,N'
重油
'
,
908277 , 45082 , 863195,'kg' ,N'SH
集团
'
,N'
郑紫至
'
)
INSERT
INTO WtBill
(BillNo,VehicleNo,GrossTime,TraceTime,CargoName
,GrossWt,TraceWt,NetWt,Unit,CustomerName,Operator)
VALUES
('20080302-0002' ,N'
粤
B66666'
,'2008-3-2 11:20:38' ,'2008-3-2 15:18:25'
,N'
大米
'
,
873644 , 57000 , 816644,'kg' ,N'JS
集团
'
,N'Peter')
38.显示绑定数据 重新设置dataGridView1的每列属性 添加参考System.Data.Linq
在代码编写之前添加两个命名空间
using
LWSMapping;
using
System.Linq;
代码如下所示(等一下再编写添加 修改 删除数据的代码 而且实现起来非常的简单):
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);
}
}
}
39
.从数据库中读取数据到用户界面和绑定数据的效果图如下
注意:打印这列数据库的
WtBill
表内不包括这个字段的
是在上面代码中添加进去的
所以很有必要使用编程的方式去控制
dataGridView