?? waredatamanage.cs
字號(hào):
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace 進(jìn)銷(xiāo)存管理系統(tǒng)
{
/// <summary>
/// WareDataManage 的摘要說(shuō)明。
/// </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 貨號(hào), 條碼, 拼音編碼, 品名, 單位, 進(jìn)貨價(jià), 銷(xiāo)售價(jià)1, 銷(xiāo)售價(jià)2, 規(guī)格, 產(chǎn)地, 類(lèi)別, 最低售價(jià) FROM 商品清單";
private System.Windows.Forms.DataGrid dgrd_WareData;
private System.Windows.Forms.ToolBar toolBar1;
private System.Windows.Forms.ToolBarButton tbarBtnSave;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.ToolBarButton tbarBtnDelete;
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.ComponentModel.IContainer components;
public WareDataManage()
{
//
// Windows 窗體設(shè)計(jì)器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
//
}
//--------重載構(gòu)造函數(shù)-----------
public WareDataManage(bool blShowToolBar)
{
//
// Windows 窗體設(shè)計(jì)器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
//
this.ds = link.SelectDataBase(sendStrSQL,sendTableName);//創(chuàng)建數(shù)據(jù)庫(kù)連接
//如果blToolBarShow為false,則表示該窗體由銷(xiāo)售單和進(jìn)貨單生成,用于雙擊選擇商品
this.DataGridStateControl(blShowToolBar);
this.dgrd_WareData.DataSource = ds.Tables[0];
this.toolBar1.Enabled = blShowToolBar;
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗體設(shè)計(jì)器生成的代碼
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(WareDataManage));
this.dgrd_WareData = new System.Windows.Forms.DataGrid();
this.toolBar1 = new System.Windows.Forms.ToolBar();
this.tbarBtnSave = new System.Windows.Forms.ToolBarButton();
this.tbarBtnDelete = new System.Windows.Forms.ToolBarButton();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
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();
((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, 74);
this.dgrd_WareData.Name = "dgrd_WareData";
this.dgrd_WareData.Size = new System.Drawing.Size(720, 352);
this.dgrd_WareData.TabIndex = 3;
this.dgrd_WareData.DoubleClick += new System.EventHandler(this.dgrd_WareData_DoubleClick);
//
// toolBar1
//
this.toolBar1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
this.tbarBtnSave,
this.tbarBtnDelete});
this.toolBar1.DropDownArrows = true;
this.toolBar1.ImageList = this.imageList1;
this.toolBar1.Location = new System.Drawing.Point(0, 0);
this.toolBar1.Name = "toolBar1";
this.toolBar1.ShowToolTips = true;
this.toolBar1.Size = new System.Drawing.Size(720, 29);
this.toolBar1.TabIndex = 4;
this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
//
// tbarBtnSave
//
this.tbarBtnSave.ImageIndex = 0;
this.tbarBtnSave.ToolTipText = "保存修改";
//
// tbarBtnDelete
//
this.tbarBtnDelete.ImageIndex = 1;
this.tbarBtnDelete.ToolTipText = "刪除數(shù)據(jù)";
//
// imageList1
//
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// groupBox1
//
this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.txt_WareWord);
this.groupBox1.Controls.Add(this.btn_Search);
this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.groupBox1.Location = new System.Drawing.Point(1, 22);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(718, 50);
this.groupBox1.TabIndex = 4;
this.groupBox1.TabStop = false;
//
// label1
//
this.label1.Location = new System.Drawing.Point(179, 22);
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(267, 18);
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(491, 17);
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 = "查詢(xún)";
this.btn_Search.Click += new System.EventHandler(this.btn_Search_Click);
//
// WareDataManage
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(720, 421);
this.Controls.Add(this.toolBar1);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.dgrd_WareData);
this.Name = "WareDataManage";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "【商品信息維護(hù)】";
((System.ComponentModel.ISupportInitialize)(this.dgrd_WareData)).EndInit();
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
//-----------設(shè)置表格格式----------
private void DataGridStateControl(bool blReadOnly)
{
DataGridTableStyle ts = new DataGridTableStyle();
ts.AlternatingBackColor = Color.LightGray;
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++)//根據(jù)DataTable中的數(shù)據(jù)類(lèi)型設(shè)置表格的樣式
{
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//設(shè)置表格樣式,如果不是供選擇,則可以選中表中的一個(gè)單元
{
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//如果是供雙擊選擇商品名稱(chēng),則單擊任意一個(gè)單元都會(huì)選中整行
{
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);
}
//--------根據(jù)商品拼音編碼查詢(xún)商品-------------
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);
}
//--------------對(duì)數(shù)據(jù)的修改,保存等操作----------------
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
if (e.Button.ToolTipText == "保存修改")//保存對(duì)數(shù)據(jù)所作的修改
{
try
{
int row = this.dgrd_WareData.CurrentCell.RowNumber;
//將當(dāng)前所在行往下移動(dòng)一行,為便于實(shí)現(xiàn)同步保存
//(否則會(huì)出現(xiàn)修改的最后一行無(wú)法保存,因?yàn)樗€處于編輯狀態(tài))
this.dgrd_WareData.CurrentCell = new DataGridCell(row+1,0);
if (this.ds.HasChanges())
{
this.link.UpdateDataBase(this.ds.GetChanges(),sendTableName);
MessageBox.Show("數(shù)據(jù)修改成功!","信息");
}
else
{
MessageBox.Show("沒(méi)有需要修改的數(shù)據(jù)!");
return;
}
}
catch
{
MessageBox.Show("數(shù)據(jù)保存失敗,請(qǐng)確認(rèn)所有信息輸入完整且正確!","提示");
return;
}
}
if (e.Button.ToolTipText == "刪除數(shù)據(jù)")//刪除某條數(shù)據(jù)
{
if (MessageBox.Show("確實(shí)要?jiǎng)h除這條記錄嗎?","詢(xún)問(wèn)",MessageBoxButtons.YesNo) == DialogResult.Yes)
{
int intRowNumber = this.dgrd_WareData.CurrentCell.RowNumber;
try
{
this.ds.Tables[0].Rows[intRowNumber].Delete();
//此處傳入只包含被改動(dòng)過(guò)的DataRow的DataSet,減少更新數(shù)據(jù)量,提高程序性能
this.link.UpdateDataBase(this.ds.GetChanges(),sendTableName);
MessageBox.Show("數(shù)據(jù)刪除成功!","信息");
}
catch
{
MessageBox.Show("該數(shù)據(jù)不能刪除!","提示");
string tempStrSQL = "select * from " + sendTableName;
this.link.SelectDataBase(tempStrSQL,sendTableName);
return;
}
}
}
}
//------------其他窗體調(diào)用商品信息窗體時(shí),雙擊選擇商品信息--------------
private void dgrd_WareData_DoubleClick(object sender, System.EventArgs e)
{
if (this.dgrd_WareData.ReadOnly == true) //判斷如果是在銷(xiāo)售單或者進(jìn)貨單中生成的窗體則響應(yīng)DataGrid的雙擊事件
{
int intCurrentRowNumber = this.dgrd_WareData.CurrentCell.RowNumber;
string sendWareID,sendWareName,sendWareUnit;
sendWareID = this.dgrd_WareData[intCurrentRowNumber,0].ToString().Trim();//保存商品編號(hào)
sendWareName = this.dgrd_WareData[intCurrentRowNumber,3].ToString().Trim();//保存商品名
sendWareUnit = this.dgrd_WareData[intCurrentRowNumber,4].ToString().Trim();//保存商品單位
string [] sendArray = new string [] {sendWareID,sendWareName,sendWareUnit};
if (blSellTableShow == true) //判斷當(dāng)前是哪個(gè)表為當(dāng)前表(銷(xiāo)售單還是售貨單)
{
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();
}
}
//---------如果是在雙擊選擇商品狀態(tài),則表格為只讀-------------
public void setDataGridReadOnly(bool blInSell)
{
this.dgrd_WareData.ReadOnly = true;
if (blInSell == true)
blSellTableShow = true;
else
blSellTableShow = false;
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -