C#實(shí)現(xiàn)銷售管理系統(tǒng)
C#制作簡(jiǎn)易的的銷售管理系統(tǒng),供大家參考,具體內(nèi)容如下
1.整體需求
1).具有簡(jiǎn)易的登錄界面
2).能對(duì)商品信息進(jìn)行快速查看、查詢、添加、編輯、保存等功能。
2.設(shè)計(jì)的窗體界面
1).登錄界面
2).商品信息的操作界面
3.所需的知識(shí)
1).C#基礎(chǔ)語(yǔ)法
2).ADO.NET數(shù)據(jù)庫(kù)
不太清楚的可以去看我主頁(yè)的文章,都是關(guān)于C#基礎(chǔ)的知識(shí)。
4.具體步驟及代碼
1).創(chuàng)建項(xiàng)目
首先打開vs2017,選擇“創(chuàng)建項(xiàng)目” ,選擇“Windows窗體應(yīng)用”。詳細(xì)的操作 可以看我之前寫的一些簡(jiǎn)單項(xiàng)目。
2).添加控件
登錄界面和商品信息界面如下:


可以試著根據(jù)圖片顯示的去添加控件,詳情見主頁(yè)的C#Windows窗體應(yīng)用設(shè)計(jì)系列。商品信息界面最上面是一個(gè)tool strip 控件。后面會(huì)把源碼發(fā)出來(lái),邊參考源碼編寫可以對(duì)C#的設(shè)計(jì)更加清楚。
3).添加代碼
需要添加的代碼如下,添加代碼的方法見主頁(yè)的文章介紹。
登錄界面:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace EMS
{
public partial class frmLogin : Form
{
BaseClass.BaseInfo baseinfo = new EMS.BaseClass.BaseInfo();
BaseClass.cPopedom popedom = new EMS.BaseClass.cPopedom();
public frmLogin()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
if (txtUserName.Text == string.Empty)
{
MessageBox.Show("用戶名稱不能為空!", "錯(cuò)誤提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
DataSet ds = null;
popedom.SysUser = txtUserName.Text;
popedom.Password = txtUserPwd.Text;
ds=baseinfo.Login(popedom);
if (ds.Tables[0].Rows.Count > 0)
{
EMS.BaseInfo.frmStock frm_Stock = new EMS.BaseInfo.frmStock();
frm_Stock.Show();
}
else
{
MessageBox.Show("用戶名稱或密碼不正確!","錯(cuò)誤提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
private void txtUserName_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13) //判斷是否按下Enter鍵
txtUserPwd.Focus();//將鼠標(biāo)焦點(diǎn)移動(dòng)到“密碼”文本框
}
private void txtUserPwd_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13)//判斷是否按下Enter鍵
btnLogin.Focus();//將鼠標(biāo)焦點(diǎn)移動(dòng)到“登錄”按鈕
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
商品主界面的代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace EMS.BaseInfo
{
public partial class frmStock : Form
{
BaseClass.BaseInfo baseinfo = new EMS.BaseClass.BaseInfo();//創(chuàng)建BaseInfo類的對(duì)象
BaseClass.cStockInfo stockinfo = new EMS.BaseClass.cStockInfo();//創(chuàng)建cStockInfo類的對(duì)象
int G_Int_addOrUpdate = 0;//定義添加/修改操作標(biāo)識(shí)
public frmStock()
{
InitializeComponent();
}
private void tlBtnAdd_Click(object sender, EventArgs e)
{
this.editEnabled();//設(shè)置各個(gè)控件的可用狀態(tài)
this.clearText();//清空文本框
G_Int_addOrUpdate = 0;//等于0為添加數(shù)據(jù)
DataSet ds = null;//創(chuàng)建數(shù)據(jù)集對(duì)象
string P_Str_newTradeCode = "";//設(shè)置庫(kù)存商品編號(hào)為空
int P_Int_newTradeCode = 0;//初始化商品編號(hào)中的數(shù)字碼
ds = baseinfo.GetAllStock("tb_stock");//獲取庫(kù)存商品信息
if (ds.Tables[0].Rows.Count == 0)//判斷數(shù)據(jù)集中是否有值
{
txtTradeCode.Text = "T1001";//設(shè)置默認(rèn)商品編號(hào)
}
else
{
P_Str_newTradeCode = Convert.ToString(ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1]["tradecode"]);//獲取已經(jīng)存在的最大編號(hào)
P_Int_newTradeCode = Convert.ToInt32(P_Str_newTradeCode.Substring(1, 4)) + 1;//獲取一個(gè)最新的數(shù)字碼
P_Str_newTradeCode = "T" + P_Int_newTradeCode.ToString();//獲取最新商品編號(hào)
txtTradeCode.Text = P_Str_newTradeCode;//將商品編號(hào)顯示在文本框中
}
}
//設(shè)置各按鈕的可用狀態(tài)
private void editEnabled()
{
groupBox1.Enabled = true;
tlBtnAdd.Enabled = false;
tlBtnEdit.Enabled = false;
tlBtnDelete.Enabled = false;
tlBtnSave.Enabled = true;
tlBtnCancel.Enabled = true;
}
//設(shè)置各按鈕的可用狀態(tài)
private void cancelEnabled()
{
groupBox1.Enabled = false;
tlBtnAdd.Enabled = true;
tlBtnEdit.Enabled = true;
tlBtnDelete.Enabled = true;
tlBtnSave.Enabled = false;
tlBtnCancel.Enabled = false;
}
//清空文本框
private void clearText()
{
txtTradeCode.Text= string.Empty;
txtFullName.Text = string.Empty;
txtType.Text = string.Empty;
txtStandard.Text = string.Empty;
txtUnit.Text = string.Empty;
txtProduce.Text = string.Empty;
}
//設(shè)置DataGridView列標(biāo)題
private void SetdgvStockListHeadText()
{
dgvStockList.Columns[0].HeaderText = "商品編號(hào)";
dgvStockList.Columns[1].HeaderText = "商品名稱";
dgvStockList.Columns[2].HeaderText = "商品型號(hào)";
dgvStockList.Columns[3].HeaderText = "商品規(guī)格";
dgvStockList.Columns[4].HeaderText = "商品單位";
dgvStockList.Columns[5].HeaderText = "商品產(chǎn)地";
dgvStockList.Columns[6].HeaderText = "庫(kù)存數(shù)量";
dgvStockList.Columns[7].Visible = false;
dgvStockList.Columns[8].HeaderText = "商品價(jià)格(加權(quán)平均價(jià)格)";
dgvStockList.Columns[9].Visible = false;
dgvStockList.Columns[10].HeaderText = "盤點(diǎn)數(shù)量";
dgvStockList.Columns[11].Visible = false;
dgvStockList.Columns[12].Visible = false;
}
private void frmStock_Load(object sender, EventArgs e)
{
txtTradeCode.ReadOnly = true;//設(shè)置商品編號(hào)文本框只讀
this.cancelEnabled();//設(shè)置各按鈕的可用狀態(tài)
//顯示所有庫(kù)存商品信息
dgvStockList.DataSource = baseinfo.GetAllStock("tb_stock").Tables[0].DefaultView;
this.SetdgvStockListHeadText();//設(shè)置DataGridView控件的列標(biāo)題
}
private void tlBtnSave_Click(object sender, EventArgs e)
{
//判斷是添加還是修改數(shù)據(jù)
if (G_Int_addOrUpdate == 0)
{
try
{
//添加數(shù)據(jù)
stockinfo.TradeCode = txtTradeCode.Text;
stockinfo.FullName = txtFullName.Text;
stockinfo.TradeType = txtType.Text;
stockinfo.Standard = txtStandard.Text;
stockinfo.Unit = txtUnit.Text;
stockinfo.Produce = txtProduce.Text;
//執(zhí)行添加操作
int id = baseinfo.AddStock(stockinfo);
MessageBox.Show("新增--庫(kù)存商品數(shù)據(jù)--成功!", "成功提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"錯(cuò)誤提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
//修改數(shù)據(jù)
stockinfo.TradeCode = txtTradeCode.Text;
stockinfo.FullName = txtFullName.Text;
stockinfo.TradeType = txtType.Text;
stockinfo.Standard = txtStandard.Text;
stockinfo.Unit = txtUnit.Text;
stockinfo.Produce = txtProduce.Text;
//執(zhí)行修改操作
int id = baseinfo.UpdateStock(stockinfo);
MessageBox.Show("修改--庫(kù)存商品數(shù)據(jù)--成功!", "成功提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
dgvStockList.DataSource = baseinfo.GetAllStock("tb_stock").Tables[0].DefaultView;//顯示最新的庫(kù)存商品信息
this.SetdgvStockListHeadText();//設(shè)置DataGridView控件列標(biāo)題
this.cancelEnabled();//設(shè)置各個(gè)按鈕的可用狀態(tài)
}
private void tlBtnEdit_Click(object sender, EventArgs e)
{
this.editEnabled();//設(shè)置各個(gè)按鈕的可用狀態(tài)
G_Int_addOrUpdate = 1;//等于1為修改數(shù)據(jù)
}
private void tlBtnFind_Click(object sender, EventArgs e)
{
if (tlCmbStockType.Text == string.Empty)//判斷查詢類別是否為空
{
MessageBox.Show("查詢類別不能為空!", "錯(cuò)誤提示!", MessageBoxButtons.OK, MessageBoxIcon.Error);
tlCmbStockType.Focus();//使查詢類別下拉列表獲得鼠標(biāo)焦點(diǎn)
return;
}
else
{
if (tlTxtFindStock.Text.Trim() == string.Empty)//判斷查詢關(guān)鍵字是否為空
{
//顯示所有庫(kù)存商品信息
dgvStockList.DataSource = baseinfo.GetAllStock("tb_stock").Tables[0].DefaultView;
this.SetdgvStockListHeadText();//設(shè)置DataGridView控件的列標(biāo)題
return;
}
}
DataSet ds = null;//創(chuàng)建DataSet對(duì)象
if (tlCmbStockType.Text == "商品產(chǎn)地") //按商品產(chǎn)地查詢
{
stockinfo.Produce = tlTxtFindStock.Text;//記錄商品產(chǎn)地
ds = baseinfo.FindStockByProduce(stockinfo, "tb_Stock");//根據(jù)商品產(chǎn)地查詢商品信息
dgvStockList.DataSource = ds.Tables[0].DefaultView;//顯示查詢到的信息
}
else//按商品名稱查詢
{
stockinfo.FullName = tlTxtFindStock.Text;//記錄商品名稱
ds = baseinfo.FindStockByFullName(stockinfo, "tb_stock");//根據(jù)商品名稱查詢商品信息
dgvStockList.DataSource = ds.Tables[0].DefaultView;//顯示查詢到的信息
}
this.SetdgvStockListHeadText();//設(shè)置DataGridView控件列標(biāo)題
}
private void tlBtnDelete_Click(object sender, EventArgs e)
{
if (txtTradeCode.Text.Trim() == string.Empty)//判斷是否選擇了商品編號(hào)
{
MessageBox.Show("刪除--庫(kù)存商品數(shù)據(jù)--失??!", "錯(cuò)誤提示!", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
stockinfo.TradeCode = txtTradeCode.Text;//記錄商品編號(hào)
int id = baseinfo.DeleteStock(stockinfo);//執(zhí)行刪除操作
MessageBox.Show("刪除--庫(kù)存商品數(shù)據(jù)--成功!", "成功提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
dgvStockList.DataSource = baseinfo.GetAllStock("tb_stock").Tables[0].DefaultView;//顯示最新的庫(kù)存商品信息
this.SetdgvStockListHeadText();//設(shè)置DataGridView控件列標(biāo)題
this.clearText();//清空文本框
}
private void tlBtnCancel_Click(object sender, EventArgs e)
{
this.cancelEnabled();//設(shè)置各個(gè)按鈕的可用狀態(tài)
}
private void dgvStockList_CellClick(object sender, DataGridViewCellEventArgs e)
{
txtTradeCode.Text = this.dgvStockList[0, dgvStockList.CurrentCell.RowIndex].Value.ToString();//顯示商品編號(hào)
txtFullName.Text = this.dgvStockList[1, dgvStockList.CurrentCell.RowIndex].Value.ToString();//顯示商品全稱
txtType.Text = this.dgvStockList[2, dgvStockList.CurrentCell.RowIndex].Value.ToString();//顯示商品型號(hào)
txtStandard.Text = this.dgvStockList[3, dgvStockList.CurrentCell.RowIndex].Value.ToString();//顯示商品規(guī)格
txtUnit.Text = this.dgvStockList[4, dgvStockList.CurrentCell.RowIndex].Value.ToString();//顯示商品單位
txtProduce.Text = this.dgvStockList[5, dgvStockList.CurrentCell.RowIndex].Value.ToString();//顯示商品產(chǎn)地
}
private void tlBtnExit_Click(object sender, EventArgs e)
{
this.Close();//關(guān)閉當(dāng)前窗體
}
private void dgvStockList_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
Main.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace EMS
{
static class Program
{
/// <summary>
/// 應(yīng)用程序的主入口點(diǎn)。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmLogin());
}
}
}
需要添加的圖片素材在源碼文件夾的icon和image文件夾里面,覺得不好看的可以自行查找。
注意,添加代碼時(shí)要與自己的添加的控件一一對(duì)應(yīng)起來(lái)。
4).建立數(shù)據(jù)庫(kù)
數(shù)據(jù)庫(kù)具體的添加方法在我主頁(yè)的文章《一起來(lái)學(xué)C#之?dāng)?shù)據(jù)庫(kù)》中講過,在菜單欄中的“項(xiàng)目”-》》“添加新項(xiàng)目”-》》“基于服務(wù)的數(shù)據(jù)庫(kù)”,具體操作可以看我前面的文章。本次給出的源碼有數(shù)據(jù)庫(kù),可以自行修改添加。

5).調(diào)試運(yùn)行
根據(jù)自己所寫的去運(yùn)行,再對(duì)照提供的源碼修改錯(cuò)誤,運(yùn)行界面如下。
登錄界面:

登錄賬戶名:mr 密碼:mrsoft,可以根據(jù)源碼自行修改。
信息界面:

寫好運(yùn)行后就可以看到該界面。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)日期格式轉(zhuǎn)換的公共方法類實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)日期格式轉(zhuǎn)換的公共方法類,結(jié)合完整實(shí)例形式分析了C#針對(duì)各種常見日期格式的轉(zhuǎn)換方法,涉及C#字符串、日期、時(shí)間相關(guān)操作技巧,需要的朋友可以參考下2017-01-01
C#操作Clipboard讀取剪切板中數(shù)據(jù)實(shí)例詳解
這篇文章主要介紹了C#操作Clipboard讀取剪切板中數(shù)據(jù)的方法,實(shí)例分析了C#讀取剪貼板數(shù)據(jù)的具體步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-05-05
C#實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出任一Word圖表的通用呈現(xiàn)方法
應(yīng)人才測(cè)評(píng)產(chǎn)品的需求,導(dǎo)出測(cè)評(píng)報(bào)告是其中一個(gè)重要的環(huán)節(jié),報(bào)告的文件類型也多種多樣,其中WORD輸出也扮演了一個(gè)重要的角色,本文給大家介紹了C#實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出任一Word圖表的通用呈現(xiàn)方法及一些體會(huì),需要的朋友可以參考下2023-10-10
C#實(shí)現(xiàn)在服務(wù)器端裁剪圖片的方法
這篇文章主要介紹了C#實(shí)現(xiàn)在服務(wù)器端裁剪圖片的方法,涉及C#操作圖片的相關(guān)技巧,需要的朋友可以參考下2015-04-04

