地磅称量系统之(37~39) 直接向数据库的数据表WtBill添加测试数据以及绑定用户控件和使用编码的方式控制dataGridView控件的每列属性和添加数据数据库的表中不存在的字段(非绑定列)

 

让知识更加联贯 让技术走进生活
我的博客        我的程序 我的网络
               ------郑紫至
               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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值