using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using OperateDb;
using System.Data.OleDb;
using System.Data;
namespace KeLeCtrl
{
 /*
  * 名称:frmDataGrid
  * 说明:
  * 版本号:
  * 作者:
  * 创建时间:
  * 修改人员:emosen
  * 修改时间:2005.3
  * 修改说明:要在表上直接操作数据库
  */
 /// <summary>
 /// frmDataGrid 的摘要说明。
 /// </summary>
 public class frmDataGrid : System.Windows.Forms.Form
 {

  #region System Fields=======================================================================
  private System.Windows.Forms.DataGrid dataGrid1;
  private System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1;
  private KeLeMain.ctrlCode.DataSet1 dataSet11;
  private System.Windows.Forms.ComboBox comboEmplNo;
//  private int lngRecordCount;
  private System.Windows.Forms.Button buttonDelAll;
  private System.Windows.Forms.Button buttonExit;
  private System.Windows.Forms.Button buttonOut;
  private System.Windows.Forms.Button buttonIn;
  private System.Windows.Forms.Panel panel1;
  private System.Windows.Forms.Splitter splitter1;
  private System.Windows.Forms.Panel panel2;
  private System.Windows.Forms.OpenFileDialog openFileDialog1;
  private System.Windows.Forms.SaveFileDialog saveFileDialog1;
  private System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
  private System.Data.OleDb.OleDbCommand oleDbInsertCommand1;
  private System.Data.OleDb.OleDbCommand oleDbUpdateCommand1;
  private System.Data.OleDb.OleDbCommand oleDbDeleteCommand1;
  private System.Data.OleDb.OleDbConnection oleDbConnection1;
  #endregion====================================================================================

  #region Custom Fields=======================================================================
  private data mydata;
  private string preEmplNo;
  private string strsql;
  private bool blnClick;//判断“发货单维护”中的产品编码框是否被MouseClick
  private bool blnUpDown;//判断“发货单维护”中的产品编码框是KeyPress上下方向键
  private bool blnNone;//productis is 不存在=true;
//  private int CurrentIndex;
//  private bool blnNo;//productis is 不存在=true;
  #endregion===================================================================================

  #region 构造函数============================================================================
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;
  public frmDataGrid()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }
  #endregion===================================================================================

  #region 清理所有正在使用的资源。============================================================
  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if(components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }
  #endregion====================================================================================

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmDataGrid));
   this.dataGrid1 = new System.Windows.Forms.DataGrid();
   this.dataSet11 = new KeLeMain.ctrlCode.DataSet1();
   this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter();
   this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand();
   this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
   this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand();
   this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
   this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand();
   this.comboEmplNo = new System.Windows.Forms.ComboBox();
   this.buttonDelAll = new System.Windows.Forms.Button();
   this.buttonExit = new System.Windows.Forms.Button();
   this.buttonOut = new System.Windows.Forms.Button();
   this.buttonIn = new System.Windows.Forms.Button();
   this.panel1 = new System.Windows.Forms.Panel();
   this.splitter1 = new System.Windows.Forms.Splitter();
   this.panel2 = new System.Windows.Forms.Panel();
   this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
   this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
   ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
   ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
   this.panel1.SuspendLayout();
   this.panel2.SuspendLayout();
   this.SuspendLayout();
   //
   // dataGrid1
   //
   this.dataGrid1.BackgroundColor = System.Drawing.Color.FromArgb(((System.Byte)(140)), ((System.Byte)(186)), ((System.Byte)(249)));
   this.dataGrid1.CaptionBackColor = System.Drawing.Color.FromArgb(((System.Byte)(140)), ((System.Byte)(186)), ((System.Byte)(249)));
   this.dataGrid1.CaptionText = "浏览";
   this.dataGrid1.DataMember = "";
   this.dataGrid1.DataSource = this.dataSet11.BarcodeOut;
   this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
   this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
   this.dataGrid1.Location = new System.Drawing.Point(0, 0);
   this.dataGrid1.Name = "dataGrid1";
   this.dataGrid1.PreferredColumnWidth = 100;
   this.dataGrid1.Size = new System.Drawing.Size(360, 341);
   this.dataGrid1.TabIndex = 1;
   this.dataGrid1.Click += new System.EventHandler(this.dataGrid1_Click);
   this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.dataGrid1_CurrentCellChanged);
   this.dataGrid1.Paint += new System.Windows.Forms.PaintEventHandler(this.dataGrid1_Paint);
   this.dataGrid1.Scroll += new System.EventHandler(this.dataGrid1_Scroll);
   //
   // dataSet11
   //
   this.dataSet11.DataSetName = "DataSet1";
   this.dataSet11.Locale = new System.Globalization.CultureInfo("zh-CN");
   //
   // oleDbDataAdapter1
   //
   this.oleDbDataAdapter1.DeleteCommand = this.oleDbDeleteCommand1;
   this.oleDbDataAdapter1.InsertCommand = this.oleDbInsertCommand1;
   this.oleDbDataAdapter1.SelectCommand = this.oleDbSelectCommand1;
   this.oleDbDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
                          new System.Data.Common.DataTableMapping("Table", "BarcodeOut", new System.Data.Common.DataColumnMapping[] {
                                                       new System.Data.Common.DataColumnMapping("产品编码", "产品编码"),
                                                       new System.Data.Common.DataColumnMapping("发货数量", "发货数量")})});
   this.oleDbDataAdapter1.UpdateCommand = this.oleDbUpdateCommand1;
   //
   // oleDbDeleteCommand1
   //
   this.oleDbDeleteCommand1.CommandText = "DELETE FROM BarcodeOut WHERE (ProductId = ?) AND (num = ? OR ? IS NULL AND num IS" +
    " NULL)";
   this.oleDbDeleteCommand1.Connection = this.oleDbConnection1;
   this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_ProductId", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "产品编码", System.Data.DataRowVersion.Original, null));
   this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_num", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "发货数量", System.Data.DataRowVersion.Original, null));
   this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_num1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "发货数量", System.Data.DataRowVersion.Original, null));
   //
   // oleDbConnection1
   //
   this.oleDbConnection1.ConnectionString = @"Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Database Password=;Data Source=""D:\code\C#\case\佛山科勒\佛山科勒\2005019\KeLeMain\bin\Debug\KeLe.mdb"";Password=;Jet OLEDB:Engine Type=5;Jet OLEDB:Global Bulk Transactions=1;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:System database=;Jet OLEDB:SFP=False;Extended Properties=;Mode=Share Deny None;Jet OLEDB:New Database Password=;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;User ID=Admin;Jet OLEDB:Encrypt Database=False";
   //
   // oleDbInsertCommand1
   //
   this.oleDbInsertCommand1.CommandText = "INSERT INTO BarcodeOut(ProductId, num) VALUES (?, ?)";
   this.oleDbInsertCommand1.Connection = this.oleDbConnection1;
   this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("ProductId", System.Data.OleDb.OleDbType.VarWChar, 50, "产品编码"));
   this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("num", System.Data.OleDb.OleDbType.VarWChar, 50, "发货数量"));
   //
   // oleDbSelectCommand1
   //
   this.oleDbSelectCommand1.CommandText = "SELECT ProductId AS 产品编码, num AS 发货数量 FROM BarcodeOut ORDER BY ProductId";
   this.oleDbSelectCommand1.Connection = this.oleDbConnection1;
   //
   // oleDbUpdateCommand1
   //
   this.oleDbUpdateCommand1.CommandText = "UPDATE BarcodeOut SET ProductId = ?, num = ? WHERE (ProductId = ?) AND (num = ? O" +
    "R ? IS NULL AND num IS NULL)";
   this.oleDbUpdateCommand1.Connection = this.oleDbConnection1;
   this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("ProductId", System.Data.OleDb.OleDbType.VarWChar, 50, "产品编码"));
   this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("num", System.Data.OleDb.OleDbType.VarWChar, 50, "发货数量"));
   this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_ProductId", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "产品编码", System.Data.DataRowVersion.Original, null));
   this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_num", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "发货数量", System.Data.DataRowVersion.Original, null));
   this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_num1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "发货数量", System.Data.DataRowVersion.Original, null));
   //
   // comboEmplNo
   //
   this.comboEmplNo.Location = new System.Drawing.Point(24, 304);
   this.comboEmplNo.Name = "comboEmplNo";
   this.comboEmplNo.Size = new System.Drawing.Size(121, 22);
   this.comboEmplNo.TabIndex = 0;
   this.comboEmplNo.KeyDown += new System.Windows.Forms.KeyEventHandler(this.comboEmplNo_KeyDown);
   this.comboEmplNo.GotFocus += new System.EventHandler(this.comboEmplNo_GotFocus);
   this.comboEmplNo.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.comboEmplNo_KeyPress);
   this.comboEmplNo.Validated += new System.EventHandler(this.comboEmplNo_Validated);
   this.comboEmplNo.TextChanged += new System.EventHandler(this.comboEmplNo_TextChanged);
   this.comboEmplNo.Click += new System.EventHandler(this.comboEmplNo_Click);
   //
   // buttonDelAll
   //
   this.buttonDelAll.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
   this.buttonDelAll.Image = ((System.Drawing.Image)(resources.GetObject("buttonDelAll.Image")));
   this.buttonDelAll.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
   this.buttonDelAll.Location = new System.Drawing.Point(56, 172);
   this.buttonDelAll.Name = "buttonDelAll";
   this.buttonDelAll.Size = new System.Drawing.Size(64, 40);
   this.buttonDelAll.TabIndex = 26;
   this.buttonDelAll.Text = "清空";
   this.buttonDelAll.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
   this.buttonDelAll.Click += new System.EventHandler(this.buttonDelAll_Click);
   //
   // buttonExit
   //
   this.buttonExit.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
   this.buttonExit.Image = ((System.Drawing.Image)(resources.GetObject("buttonExit.Image")));
   this.buttonExit.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
   this.buttonExit.Location = new System.Drawing.Point(56, 236);
   this.buttonExit.Name = "buttonExit";
   this.buttonExit.Size = new System.Drawing.Size(64, 40);
   this.buttonExit.TabIndex = 25;
   this.buttonExit.Text = "退出";
   this.buttonExit.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
   this.buttonExit.Click += new System.EventHandler(this.buttonExit_Click);
   //
   // buttonOut
   //
   this.buttonOut.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
   this.buttonOut.Image = ((System.Drawing.Image)(resources.GetObject("buttonOut.Image")));
   this.buttonOut.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
   this.buttonOut.Location = new System.Drawing.Point(56, 108);
   this.buttonOut.Name = "buttonOut";
   this.buttonOut.Size = new System.Drawing.Size(64, 40);
   this.buttonOut.TabIndex = 24;
   this.buttonOut.Text = "导出";
   this.buttonOut.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
   this.buttonOut.Click += new System.EventHandler(this.buttonOut_Click);
   //
   // buttonIn
   //
   this.buttonIn.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
   this.buttonIn.Image = ((System.Drawing.Image)(resources.GetObject("buttonIn.Image")));
   this.buttonIn.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
   this.buttonIn.Location = new System.Drawing.Point(56, 44);
   this.buttonIn.Name = "buttonIn";
   this.buttonIn.Size = new System.Drawing.Size(64, 40);
   this.buttonIn.TabIndex = 23;
   this.buttonIn.Text = "导入";
   this.buttonIn.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
   this.buttonIn.Click += new System.EventHandler(this.buttonIn_Click);
   //
   // panel1
   //
   this.panel1.Controls.Add(this.dataGrid1);
   this.panel1.Dock = System.Windows.Forms.DockStyle.Left;
   this.panel1.Location = new System.Drawing.Point(0, 0);
   this.panel1.Name = "panel1";
   this.panel1.Size = new System.Drawing.Size(360, 341);
   this.panel1.TabIndex = 27;
   //
   // splitter1
   //
   this.splitter1.Location = new System.Drawing.Point(360, 0);
   this.splitter1.Name = "splitter1";
   this.splitter1.Size = new System.Drawing.Size(3, 341);
   this.splitter1.TabIndex = 28;
   this.splitter1.TabStop = false;
   //
   // panel2
   //
   this.panel2.Controls.Add(this.buttonIn);
   this.panel2.Controls.Add(this.buttonDelAll);
   this.panel2.Controls.Add(this.buttonExit);
   this.panel2.Controls.Add(this.buttonOut);
   this.panel2.Controls.Add(this.comboEmplNo);
   this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
   this.panel2.Location = new System.Drawing.Point(363, 0);
   this.panel2.Name = "panel2";
   this.panel2.Size = new System.Drawing.Size(173, 341);
   this.panel2.TabIndex = 29;
   //
   // openFileDialog1
   //
   this.openFileDialog1.DefaultExt = "txt";
   this.openFileDialog1.FileName = "ProductId.txt";
   this.openFileDialog1.Filter = "txt文件(*.txt)|*.txt";
   this.openFileDialog1.Title = "选择导入的产品编号文档";
   //
   // saveFileDialog1
   //
   this.saveFileDialog1.DefaultExt = "txt";
   this.saveFileDialog1.FileName = "check.txt";
   this.saveFileDialog1.Filter = "txt文件(*.txt)|*.txt";
   this.saveFileDialog1.Title = "导出Check.txt(条形码,数量)文档";
   //
   // frmDataGrid
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(7, 16);
   this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(140)), ((System.Byte)(186)), ((System.Byte)(249)));
   this.ClientSize = new System.Drawing.Size(536, 341);
   this.Controls.Add(this.panel2);
   this.Controls.Add(this.splitter1);
   this.Controls.Add(this.panel1);
   this.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
   this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
   this.Name = "frmDataGrid";
   this.ShowInTaskbar = false;
   this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
   this.Text = "发货单维护";
   this.Closing += new System.ComponentModel.CancelEventHandler(this.frmDataGrid_Closing);
   this.Load += new System.EventHandler(this.frmDataGrid_Load);
   ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
   ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();
   this.panel1.ResumeLayout(false);
   this.panel2.ResumeLayout(false);
   this.ResumeLayout(false);

  }
  #endregion

  #region frmDataGrid_Load=====================================================================
  private void frmDataGrid_Load(object sender, System.EventArgs e)
  {
   oleDbConnection1.ConnectionString="Provider=Microsoft.JET.OLEDB.4.0;data source="+Application.StartupPath+"\\kele.mdb";
   int x,y;
   comboEmplNo.Width=dataGrid1.PreferredColumnWidth;
   x=dataGrid1.Location.X+dataGrid1.RowHeaderWidth+1 ;
   y=dataGrid1.Location.Y+2*dataGrid1.PreferredRowHeight+12;
   comboEmplNo.Location=new Point(x,y);
   comboEmplNo.Focus();

   comboEmplNo.Width = 0;
   comboEmplNo.MaxDropDownItems =8;
   dataGrid1.PreferredRowHeight =comboEmplNo.Height;

   dataGrid1.Controls.Add(comboEmplNo);

   comboEmplNo.Focus();

   oleDbDataAdapter1.Fill(dataSet11);
//   if(dataSet11.Tables[0].Rows.Count>0)
   SendKeys.Send("{ENTER}");
   
   try
   {
    comboEmplNo.Text=dataGrid1[0,0].ToString();
    preEmplNo=comboEmplNo.Text.Trim() ;
   }
   catch
   {;}

   strsql="select productid from barcodeData";
   DataTable myTempDt=ReadRecord(strsql);
   comboEmplNo.Items.Clear();
   for(int i=0;i<myTempDt.Rows.Count;i++)
   {
    comboEmplNo.Items.Add(myTempDt.Rows[i][0].ToString());
   }
  
   mydata=new data();
   mydata.OpenDb();
  }
  #endregion======================================================================================
  
  #region frmDataGrid_Closing==================================================================
  private void frmDataGrid_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  {
   try
   {
    oleDbDataAdapter1.Update(dataSet11);
   }
   catch
   {;}
   DataSet myds=GetRecordCount("select * from barcodeout");
   if(myds.Tables[0].Rows.Count>0)
   {
    if(MessageBox.Show("确定有导出CHECK.TXT文件吗?","提示信息",MessageBoxButtons.YesNo,MessageBoxIcon.Information)==DialogResult.Yes)
    {
     mydata.CloseDb();
     try
     {
      if(dataGrid1.CurrentCell.RowNumber==0)
      {
       GetFocus(1,0);
      }
      else
      {
       GetFocus(0,0);
      }
     }
     catch
     {;}

     try
     {
      oleDbDataAdapter1.Update(dataSet11);
     }
     catch
     {;}
     oleDbDataAdapter1.Fill(dataSet11);
     oleDbDataAdapter1.Dispose();
     oleDbConnection1.Close();
    }
    else
    {
     e.Cancel=true;
     buttonOut.Focus();
    }
   }
  }
  #endregion======================================================================================

  #region GetFocus()===========================================================================
  private void GetFocus(int row,int col)
  {
   //先把焦点移动到DataGrid
   this.dataGrid1.Focus();
   //把焦点移动到DataGridCell
   DataGridCell dgc = new DataGridCell(row,col);
   this.dataGrid1.CurrentCell = dgc; //move to this cell
//   MessageBox.Show("datagrid");
//   DataGridTextBoxColumn dgtb = (DataGridTextBoxColumn)dataGrid1.TableStyles[0].GridColumnStyles[col];
//   //设置焦点
//   dgtb.TextBox.Focus();
  }
  #endregion======================================================================================

  #region dataGrid1_CurrentCellChanged=========================================================
  private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
  {
   if(blnNone==false)
   {
    if(dataGrid1.CurrentCell.ColumnNumber==0)
    {
     comboEmplNo.Visible =false;
     comboEmplNo.Width = 0;
     comboEmplNo.MaxDropDownItems =8;
     comboEmplNo.Left = dataGrid1.GetCurrentCellBounds().Left;
     comboEmplNo.Top = dataGrid1.GetCurrentCellBounds().Top;
     comboEmplNo.Text = dataGrid1[dataGrid1.CurrentCell.RowNumber,dataGrid1.CurrentCell.ColumnNumber].ToString();
     preEmplNo=comboEmplNo.Text;
     comboEmplNo.Visible =true;
     comboEmplNo.Focus();
     if(!blnNone) SendKeys.Send("{ENTER}");
     //    comboEmplNo.SelectAll();
    }
    else
    {
     comboEmplNo.Visible = false;
     comboEmplNo.Width = 0;
     comboEmplNo.MaxDropDownItems =8;
    }
   }
   else
   {
    MessageBox.Show("该'产品编码'在《产品编码和条形码信息维护》中不存在!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
    comboEmplNo.Focus();
    comboEmplNo.SelectAll();
   }
  }
  #endregion======================================================================================

  #region dataGrid1_Scroll=====================================================================
  private void dataGrid1_Scroll(object sender, System.EventArgs e)
  {
   comboEmplNo.Visible = false;
   comboEmplNo.Width = 0;
   comboEmplNo.MaxDropDownItems =8;
  }
  #endregion======================================================================================

  #region dataGrid1_Click======================================================================
  private void dataGrid1_Click(object sender, System.EventArgs e)
  {
   comboEmplNo.Visible = false;
   comboEmplNo.Width = 0;
   comboEmplNo.MaxDropDownItems =8;
  }
  #endregion======================================================================================

  #region comboEmplNo_TextChanged==============================================================
  private void comboEmplNo_TextChanged(object sender, System.EventArgs e)
  {
   if(!(blnClick || blnUpDown))
   {
    string strsql="select productid from barcodeData where productid like '"+comboEmplNo.Text.Trim()+"%'";
    DataTable myTempDt=ReadRecord(strsql);
    comboEmplNo.Items.Clear();
    for(int i=0;i<myTempDt.Rows.Count;i++)
    {
     comboEmplNo.Items.Add(myTempDt.Rows[i][0].ToString());
    }
    comboEmplNo.MaxDropDownItems=8;
    comboEmplNo.SelectionStart=comboEmplNo.Text.Trim().Length;
   }
//   comboEmplNo's value to Datagrid1.cell
//   if (dataGrid1[dataGrid1.CurrentCell.RowNumber,dataGrid1.CurrentCell.ColumnNumber].ToString().Trim()+ "" == "")
//   {
//    SendKeys.Send("*");
////    dataGrid1[dataGrid1.CurrentCell.RowNumber,dataGrid1.CurrentCell.ColumnNumber]="**";
//   }
   try
   {
    dataGrid1[dataGrid1.CurrentCell.RowNumber,dataGrid1.CurrentCell.ColumnNumber]=comboEmplNo.Text.ToString();
   }
   catch
   {;}
   try
   {
    if (dataGrid1[dataGrid1.CurrentCell.RowNumber,dataGrid1.CurrentCell.ColumnNumber].ToString().Trim()+ "" == "")
    {
     dataGrid1[dataGrid1.CurrentCell.RowNumber,dataGrid1.CurrentCell.ColumnNumber]="*";
    }

   }
   catch
   {
    ;
   }
  }
  #endregion======================================================================================

  #region comboEmplNo_KeyPress=================================================================
  private void comboEmplNo_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
  {
   comboEmplNo.DroppedDown=true;
   blnClick=false;
   blnUpDown=false;

   if(e.KeyChar==13)
   {
    if(comboEmplNo.Text.Trim()!="")
    {
     //     int idx = comboEmplNo.FindString(comboEmplNo.Text.Trim(),0);
     if(comboEmplNo.Items.Count>0)
      comboEmplNo.SelectedIndex = 0;
     

     strsql="select productid from barcodeData where productid='"+comboEmplNo.Text.ToString().Trim()+"'";
     DataTable myTempDt=ReadRecord(strsql);
     if(myTempDt.Rows.Count==0)
     {
      blnNone=true;
      //      e.Handled=true;
      MessageBox.Show("该'产品编码'在《产品编码和条形码信息维护》中不存在!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
      comboEmplNo.Text="";
      comboEmplNo.Focus();
      comboEmplNo.Width=0;
      comboEmplNo.MaxDropDownItems=8;
      myTempDt.Dispose();
      return;
     }
     blnNone=false;
     myTempDt.Dispose();
    }
    else
    {
     MessageBox.Show("'产品编码'不可以为空!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
     comboEmplNo.Focus();
     comboEmplNo.Width=0;
     comboEmplNo.MaxDropDownItems=8;
     return;
    }
    try
    {
     GetFocus(dataGrid1.CurrentCell.RowNumber,dataGrid1.CurrentCell.ColumnNumber+1);
    }
    catch
    {;}    
   }
  }
  #endregion======================================================================================

  #region comboEmplNo_Validated================================================================
  private void comboEmplNo_Validated(object sender, System.EventArgs e)
  {
   try
   {
    if(comboEmplNo.Text.Trim()!="")
    {
     //     CurrentIndex=dataGrid1.CurrentCell.RowNumber;
     //     int idx = comboEmplNo.FindString(comboEmplNo.Text.Trim(),0);
     //     comboEmplNo.SelectedIndex = idx;
     strsql="select productid from barcodeData where productid='"+comboEmplNo.Text.Trim()+"'";
     DataTable myTempDt=ReadRecord(strsql);
     if(myTempDt.Rows.Count==0)
     {
      blnNone=true;
      MessageBox.Show("该'产品编码'在《产品编码和条形码信息维护》中不存在!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
      comboEmplNo.Focus();
      comboEmplNo.Width=0;
      comboEmplNo.MaxDropDownItems=8;
     }
     myTempDt.Dispose();
     blnNone=false;
    }
//    else
//    {
//     MessageBox.Show("'产品编码'不可以为空!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
//     comboEmplNo.Focus();
//     comboEmplNo.Width=0;
//     comboEmplNo.MaxDropDownItems=8;
//     comboEmplNo.Text="";
//    }
   }
   catch
   {;}
  }
  #endregion======================================================================================

  #region dataGrid1_Paint======================================================================
  private void dataGrid1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {
   if(dataGrid1.CurrentCell.ColumnNumber==0)
   {
    comboEmplNo.Width = dataGrid1.GetCurrentCellBounds().Width;
   }
  }
  #endregion======================================================================================

  #region ProcessCmdKey通过回车键移到下一控件==================================================
  protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
  {
   if(keyData == Keys.Enter)//通过回车键移到下一控件
   {
    if(this.ActiveControl.Parent is DataGrid && !(this.ActiveControl is ComboBox))
    {
//     MessageBox.Show("right");
     SendKeys.Send("{RIGHT}");
     comboEmplNo.Focus();
     comboEmplNo.SelectAll();
     return true;
    }
   }
//   if(keyData == Keys.Delete)//通过回车键移到下一控件
//   {
//    if(this.ActiveControl is DataGrid)
//    {
//     if(MessageBox.Show("确定删除吗?","提示信息",MessageBoxButtons.YesNo,MessageBoxIcon.Information)==DialogResult.Yes)
//     {
//      SendKeys.Send("{DELETE}");
//      return true;
//     }
//     else
//     {
//      return false;
//     }
//    }
//   }
   return base.ProcessCmdKey(ref msg, keyData);
  }
  #endregion======================================================================================

  #region comboEmplNo_GotFocus=================================================================
  private void comboEmplNo_GotFocus(object sender, EventArgs e)
  {
//   comboEmplNo.Select(0,comboEmplNo.Text.ToString().Trim().Length);
  }
  #endregion======================================================================================
  
  #region 判断某记录是否存在===================================================================
  //判断记录是否存在
  public bool dataReader(string strsql)
  {
   OleDbCommand cmdExist =new OleDbCommand(strsql,oleDbConnection1);
   cmdExist.Connection.Open();
   OleDbDataReader selreader ;
   selreader=cmdExist.ExecuteReader();
   bool ifread;
   if (selreader.Read())
    ifread=true;
   else
    ifread=false;
   selreader.Close();
   cmdExist.Connection.Close();
   cmdExist.Dispose();
   return ifread; 
  }
  #endregion====================================================================================

  #region read record==========================================================================
  //ReadRecord===============================================================
  public DataTable ReadRecord(string strSql)
  {
   #region code==========================================================
   OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSql,oleDbConnection1);
   DataTable mydt=new DataTable();
   mydt.Reset();
   objAdapter.Fill(mydt);
   objAdapter.Dispose();
   return mydt;
   #endregion =============================================================
  }
  #endregion====================================================================================

  #region Get RecordCount======================================================================
  public DataSet GetRecordCount(string strsql)
  {
   OleDbDataAdapter objAdapter = new OleDbDataAdapter(strsql,oleDbConnection1);
   DataSet myds=new DataSet();
   myds.Reset();
   objAdapter.Fill(myds);
   objAdapter.Dispose();
   return myds;
  }
  #endregion====================================================================================

  #region debug test===========================================================================
  private void button1_Click(object sender, System.EventArgs e)
  {
   try
   {
    GetFocus(4,0);
   }
   catch
   {;}
   MessageBox.Show(dataGrid1.CurrentCell.RowNumber.ToString());
   MessageBox.Show(dataGrid1.CurrentCell.ColumnNumber.ToString());
  }

  private void button2_Click(object sender, System.EventArgs e)
  {
   MessageBox.Show(dataGrid1.CurrentCell.RowNumber.ToString());
   MessageBox.Show(dataGrid1.CurrentCell.ColumnNumber.ToString());
  }

  private void button3_Click(object sender, System.EventArgs e)
  {
   string strsql;
   strsql="select * from barcodedata where productid='"+"2"+"'";
   bool blnExist=dataReader(strsql);
   MessageBox.Show(blnExist.ToString());
  }

  private void button4_Click(object sender, System.EventArgs e)
  {
   GetFocus(3,0);
  }
  #endregion================================================================================

  #region buttonIn_Click导入===================================================================
  private void buttonIn_Click(object sender, System.EventArgs e)
  {
   string strPath,strTxtName,strTemp;
   buttonIn.Enabled=false;
   
   openFileDialog1.Title="导入'条形码'和'数量'的文档";
   openFileDialog1.FileName="Barcode.txt";
   openFileDialog1.InitialDirectory=Application.StartupPath;
   if(openFileDialog1.ShowDialog()==DialogResult.OK)
   {
    strTemp=openFileDialog1.FileName.ToString();
    strPath=strTemp.Substring(0,strTemp.LastIndexOf("\\")+1);
    strTxtName=strTemp.Substring(strTemp.LastIndexOf("\\")+1);
    if(mydata.InTxt(strPath,strTxtName,"barcodeOut"))
    {
     MessageBox.Show("导入成功!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
    }
    else
    {
     MessageBox.Show("重复的'产品编码'或不存在该'产品编码',造成导入失败!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
    }
    oleDbDataAdapter1.Update(dataSet11);
    dataGrid1.DataSource=dataSet11.Tables[0].DefaultView;
//    dataSet11.Reset();
//    dataGrid1.Refresh();
    strsql="select productid as 产品编码,num as 发货数量 from BarcodeOut order by productid";
    mydata.dgBind(strsql,dataGrid1,"barcodeOut");
    this.Close();
//    dataSet11.Reset();
//    dataGrid1.Refresh();
   }
   buttonIn.Enabled=true;
  }
  #endregion======================================================================================

  #region buttonOut_Click导出==================================================================
  private void buttonOut_Click(object sender, System.EventArgs e)
  {
   string strTxtName="";
   buttonOut.Enabled=false;
   
   saveFileDialog1.InitialDirectory=Application.StartupPath;
   if(saveFileDialog1.ShowDialog()==DialogResult.OK)
   {
    strsql="select BarcodeData.barcode,num from BarcodeOut,BarcodeData where BarcodeOut.Productid=BarcodeData.productid order by BarcodeData.barcode";
    strTxtName=saveFileDialog1.FileName.ToString();
   }
   else
   {
    buttonOut.Enabled=true;
    return;
   }

   if(mydata.OutTxt(strTxtName,strsql))
   {
    MessageBox.Show("导出成功!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
   }
   else
   {
    MessageBox.Show("导出失败!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
   }
   buttonOut.Enabled=true;
  }
  #endregion======================================================================================

  #region  buttonDelAll_Click清空===============================================================
  private void buttonDelAll_Click(object sender, System.EventArgs e)
  {
//   if(MessageBox.Show("删除所有资料前,已经导出'Check.txt'文档吗?","警告",MessageBoxButtons.YesNo,MessageBoxIcon.Warning)==DialogResult.Yes)
//   {
//    dataSet11.Clear();
//    oleDbDataAdapter1.Update(dataSet11);
//    dataGrid1.Refresh();
//   }
//   else
//   {
//    buttonOut.Focus();
//   }

   comboEmplNo.Visible=false;
   try
   {
    oleDbDataAdapter1.Update(dataSet11);
   }
   catch
   {;}

   DataSet myds=GetRecordCount("select * from barcodeout");
   if(myds.Tables[0].Rows.Count==0)
    return;

   
   if(MessageBox.Show("删除所有资料前,已经导出'Check.txt'文档吗?","警告",MessageBoxButtons.YesNo,MessageBoxIcon.Warning)==DialogResult.Yes)
   {
    buttonDelAll.Enabled=false;
    strsql="delete from barcodeOut";
    bool bmod=mydata.ModifyData(strsql);
    if(bmod)
    {
     strsql="select productid as 产品编码,num as 发货数量 from barcodeOut order by productid";
     mydata.dgBind(strsql,dataGrid1,"barcodeData");
     MessageBox.Show("删除成功!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
     this.Close();
    }
    else
    {
     MessageBox.Show("删除失败!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
    }
   }
   else
   {
    buttonOut.Focus();
   }
   buttonDelAll.Enabled=true;
  }
  #endregion======================================================================================

  #region buttonExit_Click退出=================================================================
  private void buttonExit_Click(object sender, System.EventArgs e)
  {
   this.Close();
  }
  #endregion======================================================================================

  #region comboEmplNo_Click====================================================================
  private void comboEmplNo_Click(object sender, System.EventArgs e)
  {
   blnClick=true;
  }
  #endregion====================================================================================

  #region comboEmplNo_KeyDown上下键============================================================
  private void comboEmplNo_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
  {
   if (e.KeyCode==Keys.Up || e.KeyCode==Keys.Down)
   {
    blnUpDown=true;
   }
  }
  #endregion===================================================================================
 }
}

posted on 2005-03-21 16:12  Emosen  阅读(1639)  评论(0编辑  收藏  举报