?? waredatamanage.cs
字號:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace 進銷存管理系統
{
/// <summary>
/// WareDataManage 的摘要說明。
/// </summary>
public class WareDataManage : System.Windows.Forms.Form
{
private bool blSellTableShow;
private DataSet ds = new DataSet();
private LinkDataBase link = new LinkDataBase();
private string sendTableName = "商品清單";
private string sendStrSQL = "SELECT 貨號, 條碼, 拼音編碼, 品名, 單位, 進貨價, 銷售價1, 銷售價2, 規格, 產地, 類別, 最低售價 FROM 商品清單";
private System.Windows.Forms.DataGrid dgrd_WareData;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txt_WareWord;
private System.Windows.Forms.Button btn_Search;
private System.Windows.Forms.Button btnSave;
private System.Windows.Forms.Button btnDel;
private System.ComponentModel.IContainer components;
public WareDataManage()
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調用后添加任何構造函數代碼
//
}
//--------重載構造函數-----------
public WareDataManage(bool blShowToolBar)
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調用后添加任何構造函數代碼
//
this.ds = link.SelectDataBase(sendStrSQL,sendTableName);//創建數據庫連接
//如果blToolBarShow為false,則表示該窗體由銷售單和進貨單生成,用于雙擊選擇商品
this.DataGridStateControl(blShowToolBar);
this.dgrd_WareData.DataSource = ds.Tables[0];
this.btnSave.Enabled = blShowToolBar;
this.btnDel.Enabled = blShowToolBar;
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.dgrd_WareData = new System.Windows.Forms.DataGrid();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label1 = new System.Windows.Forms.Label();
this.txt_WareWord = new System.Windows.Forms.TextBox();
this.btn_Search = new System.Windows.Forms.Button();
this.btnSave = new System.Windows.Forms.Button();
this.btnDel = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dgrd_WareData)).BeginInit();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// dgrd_WareData
//
this.dgrd_WareData.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.dgrd_WareData.CaptionVisible = false;
this.dgrd_WareData.DataMember = "";
this.dgrd_WareData.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dgrd_WareData.Location = new System.Drawing.Point(0, 56);
this.dgrd_WareData.Name = "dgrd_WareData";
this.dgrd_WareData.Size = new System.Drawing.Size(720, 376);
this.dgrd_WareData.TabIndex = 3;
this.dgrd_WareData.DoubleClick += new System.EventHandler(this.dgrd_WareData_DoubleClick);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.btnDel);
this.groupBox1.Controls.Add(this.btnSave);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.txt_WareWord);
this.groupBox1.Controls.Add(this.btn_Search);
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top;
this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.groupBox1.Location = new System.Drawing.Point(0, 0);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(720, 48);
this.groupBox1.TabIndex = 4;
this.groupBox1.TabStop = false;
//
// label1
//
this.label1.Location = new System.Drawing.Point(96, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 16);
this.label1.TabIndex = 5;
this.label1.Text = "商品拼音:";
//
// txt_WareWord
//
this.txt_WareWord.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.txt_WareWord.Location = new System.Drawing.Point(184, 16);
this.txt_WareWord.Name = "txt_WareWord";
this.txt_WareWord.Size = new System.Drawing.Size(200, 21);
this.txt_WareWord.TabIndex = 1;
this.txt_WareWord.Text = "";
//
// btn_Search
//
this.btn_Search.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.btn_Search.Location = new System.Drawing.Point(410, 16);
this.btn_Search.Name = "btn_Search";
this.btn_Search.Size = new System.Drawing.Size(48, 24);
this.btn_Search.TabIndex = 2;
this.btn_Search.Text = "查詢";
this.btn_Search.Click += new System.EventHandler(this.btn_Search_Click);
//
// btnSave
//
this.btnSave.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.btnSave.Location = new System.Drawing.Point(472, 16);
this.btnSave.Name = "btnSave";
this.btnSave.Size = new System.Drawing.Size(48, 23);
this.btnSave.TabIndex = 6;
this.btnSave.Text = "保存";
this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
//
// btnDel
//
this.btnDel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.btnDel.Location = new System.Drawing.Point(536, 16);
this.btnDel.Name = "btnDel";
this.btnDel.Size = new System.Drawing.Size(48, 23);
this.btnDel.TabIndex = 7;
this.btnDel.Text = "刪除";
this.btnDel.Click += new System.EventHandler(this.btnDel_Click);
//
// WareDataManage
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(720, 421);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.dgrd_WareData);
this.Name = "WareDataManage";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "商品信息維護";
((System.ComponentModel.ISupportInitialize)(this.dgrd_WareData)).EndInit();
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
//設置表格格式
private void DataGridStateControl(bool blReadOnly)
{
DataGridTableStyle ts = new DataGridTableStyle();
ts.AlternatingBackColor = Color.SkyBlue;
ts.HeaderBackColor = Color.SkyBlue;
ts.MappingName = this.ds.Tables[0].TableName;
ts.AllowSorting = false;
int numCols = this.ds.Tables[0].Columns.Count;
for (int i = 0;i< numCols;i++)
{
if (blReadOnly == true)
{
if ( i == 4 )
{
DataTable tempTable = new DataTable();
tempTable.Columns.Add();
object[] rowValues = new object[] {"包","瓶","盒","帶","箱"};
for (int j=0;j<5;j++)
{
tempTable.Rows.Add(tempTable.NewRow());
tempTable.Rows[j][0] = rowValues[j];
}
DataGridComboBoxColumn aComboBoxColumn = new DataGridComboBoxColumn(tempTable,0,0);
aComboBoxColumn.MappingName = this.ds.Tables[0].Columns[i].ColumnName;
aComboBoxColumn.HeaderText = this.ds.Tables[0].Columns[i].ColumnName;
aComboBoxColumn.NullText = "";
ts.GridColumnStyles.Add(aComboBoxColumn);
}
else
{
DataGridTextBoxColumn aColumnTextColumn = new DataGridTextBoxColumn();
aColumnTextColumn.MappingName = this.ds.Tables[0].Columns[i].ColumnName;
aColumnTextColumn.HeaderText = this.ds.Tables[0].Columns[i].ColumnName;
aColumnTextColumn.NullText = "";
aColumnTextColumn.Format = "F";
ts.GridColumnStyles.Add(aColumnTextColumn);
}
}
else
{
DataGridNoActiveCellColumn aColumnTextColumn = new DataGridNoActiveCellColumn();
aColumnTextColumn.MappingName = this.ds.Tables[0].Columns[i].ColumnName;
aColumnTextColumn.HeaderText = this.ds.Tables[0].Columns[i].ColumnName;
aColumnTextColumn.NullText = "";
aColumnTextColumn.Format = "F";
ts.GridColumnStyles.Add(aColumnTextColumn);
}
}
this.dgrd_WareData.TableStyles.Add(ts);
}
//根據商品拼音編碼查詢商品
private void btn_Search_Click(object sender, System.EventArgs e)
{
string strSpellWord = this.txt_WareWord.Text.Trim();
string tempStrSQL = "select * from 商品清單 where 拼音編碼 like '" + strSpellWord + "%'";
this.link.SelectDataBase(tempStrSQL,sendTableName);
}
//其他窗體調用商品信息窗體時,雙擊選擇商品信息
private void dgrd_WareData_DoubleClick(object sender, System.EventArgs e)
{
if (this.dgrd_WareData.ReadOnly == true)
{
int intCurrentRowNumber = this.dgrd_WareData.CurrentCell.RowNumber;
string sendWareID = this.dgrd_WareData[intCurrentRowNumber,0].ToString().Trim();
string sendWareName = this.dgrd_WareData[intCurrentRowNumber,3].ToString().Trim();
string sendWareUnit = this.dgrd_WareData[intCurrentRowNumber,4].ToString().Trim();
string [] sendArray = new string [] {sendWareID,sendWareName,sendWareUnit};
if (blSellTableShow == true)
{
SellTable.inputDataGridArray[0] = sendArray[0];
SellTable.inputDataGridArray[1] = sendArray[1];
SellTable.inputDataGridArray[2] = sendArray[2];
}
else
{
StockTable.inputDataGridArray[0] = sendArray[0];
StockTable.inputDataGridArray[1] = sendArray[1];
StockTable.inputDataGridArray[2] = sendArray[2];
}
this.Close();
}
}
//如果是在雙擊選擇商品狀態,則表格為只讀
public void setDataGridReadOnly(bool blInSell)
{
this.dgrd_WareData.ReadOnly = true;
if (blInSell == true)
blSellTableShow = true;
else
blSellTableShow = false;
}
private void btnSave_Click(object sender, System.EventArgs e)
{
try
{
int row = this.dgrd_WareData.CurrentCell.RowNumber;
this.dgrd_WareData.CurrentCell = new DataGridCell(row+1,0);
if (this.ds.HasChanges())
{
this.link.UpdateDataBase(this.ds.GetChanges(),sendTableName);
MessageBox.Show("數據修改成功!","信息");
}
else
{
MessageBox.Show("沒有需要修改的數據!");
return;
}
}
catch
{
MessageBox.Show("數據保存失敗,請確認所有信息輸入完整且正確!","提示");
return;
}
}
private void btnDel_Click(object sender, System.EventArgs e)
{
if (MessageBox.Show("確實要刪除這條記錄嗎?","詢問",MessageBoxButtons.YesNo) == DialogResult.Yes)
{
int intRowNumber = this.dgrd_WareData.CurrentCell.RowNumber;
try
{
this.ds.Tables[0].Rows[intRowNumber].Delete();
this.link.UpdateDataBase(this.ds.GetChanges(),sendTableName);
MessageBox.Show("數據刪除成功!","信息");
}
catch
{
MessageBox.Show("該數據不能刪除!","提示");
string tempStrSQL = "select * from " + sendTableName;
this.link.SelectDataBase(tempStrSQL,sendTableName);
return;
}
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -