=================超市管理系統--商品管理========================================
https://ke.qq.com/webcourse/index.html#cid=358360&term_id=100425955&taid=2947601695864792&vid=t1430sa62c4
涉及的技術點有
windows form基本控件、linq、dapper、簡單的sql
控件復制按住Ctrl鼠標拖拽
winForm項目改為類庫項目
右鍵項目屬性輸出類型==類庫
刪除program.cs和Form.cs
dataGridView設置不顯示列字段名
dgvGoods.AutoGenerateColumns=false;
添加右鍵菜單
選擇要添加的控件(如dageGridView)
選擇菜單和工具欄===ContextMenuStrip
在dageGridView修改屬性ContextMenuStrip
1、新建一個窗體應用
1、form屬性--text=超市管理系統
2、添加一個容器菜單--菜單和工具欄MenuStrip
添加用戶管理--本地業務--系統
本地業務下添加商品管理
3、修改form屬性isMdiContainer==true
設置窗體是否是MDI窗體,MDI(Multiple Document Interface) 就是所謂的多文檔界面,所以在做多文檔界面程序時要用.MDI父窗體相應的所有子窗體都要設置MdiParent屬性.子窗體永遠都將在MDI父窗體框架之內.
比如
FormA: IsMdiContainer = true; //MDI父窗體
FormB: IsMdiContainer = false; //子窗體
MdiParent = FormA;
4、在項目右鍵添加winform窗體
5、在商品管理上加點擊事件
GoodsManagerForm form = new GoodsManagerForm();
form.MdiParent = this;
form.WindowState = FormWindowState.Maximized;
form.Show();
6、設置mainForm屬性WindowState=Maximized;
7、添加一個panel -設置停靠left
在panel中添加groupBox
設置停靠left,groupBox的text屬性=增加商品信息
增加label==商品編碼
textBox==
latel==*(這是一個提示:必填)foreColor設置紅色
供應商修改為下拉控件combobox
修改樣式dropDownStyle==dropDownList
進貨價修改為數字控件numericUpDown
設置DecimalPlaces(小數點位)
添加一個dataGridView 設置停靠center
添加列
設置屬性AllowUserToAddRows==false不允許增加行
設置屬性AllowUserToDeleteRows==false不允許刪除行
設置屬性AllowUserToResizeRows==false不允許改變行尺寸
RowHeadersBordersVisible=false 不顯示行頭
ReadOnly==true
Multiselect=false
數據庫表
GoodsTab
ID
GTid
Sup_ID
GCode
GName
BuyingPrice
RetailPrice
Unit
Specifications
StorageLocation
代碼實現
在解決方案下添加項目類庫
Lanhui.Sys.Supermarket.Dal
添加類BaseDal
private static string connectionString = "server=.;database=SupermarketManager;uid=sa;pwd=123456";
public IDbConnection Connection{get;set;}
public BaseDal(){
Connection = new SqlConnection(connectionString);
Connection.Open();
}
添加引用(管理NuGet程序包Dapper 版本1.50.2支持frameWork4.5)
Lanhui.Sys.Supermarket.Service
添加基類BaseService
添加引用:Lanhui.Sys.Supermarket.Dal,Lanhui.Sys.Supermarket.Model
添加引用(管理NuGet程序包Dapper 版本1.50.2支持frameWork4.5)
public BaseDal Dal{get;set;}
public BaseService(){
Dal = new BasDal();
}
添加商品類GoodsService:BaseService(繼承基類)
public List<GoodsTab> SearchGoods(String GCode,int GTid){
using(Dal.Connection){
string sql=string.Format("select a.*,b.GTName,c.SName,d.StorageNums,d.SupermarketNums,d.NotInStorageNums from GoodsTab a left join GoodTypeTab b on a.ID=d.GId,SupplierInfoTab c,GoodsStockTab d where a.GTid=b.ID and a.Sup_ID=c.ID and a.GCode like '%{0}%' and {1}",GCode, GTid==0?"1=1":$"a.GTiid={GTid}")
return Dal.Connection.Query<GoodsTab>(sql).ToList();
}
}
public bool IsAdd(string gcode){
using(Dal.Connection){
string sql = $"select count(*) from GoodsTab where GCode={gcode}";
return Dal.Connection.Query<int>(sql).FirstOrDefault()>0
}
}
public bool Add(GoodsTab goods){
using(Dal.Connection){
string sql="insert into GoodsTab values (@GTid,@Sup_Id,@GCode)";
int result = Dal.Connection.Execute(sql,goods);
return result>0;
}
}
public bool Delete(int ID,ref string message){
string sql = $"select count(*) from GoodsStockTab where Gid = {ID}";
int count = Dal.Connection.Query<int>(sql).FirstOrDefault();
if(count>0){
message="該商品已經使用過,不能刪除";
reutrn false;
}
sql = $"delete from GoodsTab where ID={ID}";
int result = Dal.Connection.Execute(sql);
reutrn result>0;
}
Lanhui.Sys.Supermarket.Model
添加實體類GoodsTab
public int ID{get;set;}
//額外的屬性
public int StorageNums{get;set;}
public int GTName{get;set;}
在GoodManangerForm.cs中點擊查詢事件
在項目Lanhui.Sys.Supermarket下
添加引用:Lanhui.Sys.Supermarket.Dal,Lanhui.Sys.Supermarket.Model
private GoodsService service => new GoodsServcie();//表示每回使用service時都會實例化GoodsService()
private void Init(){
//初始化方法
var GTInfo = Service.GetGoodsTypeTabs();
GTInfos.Insert(0,Model.GoodsTypeTab(){GTName="請選擇"});
cboGoodType_s.DataSource=GTInfos;
//添加供應商的下拉數據和商品類型
var GTInfos2 = new List<Model.GoodsTypeTab>();
GTInfos2.AddRange(GIInfos);
cboGoodType.DataSource=GTInfos2;
}
private void Search(){
var datas = Service.SearchGoods(txtGCode_S.text.trim(),(int)cboGoodType_s.SelectedValue)
dgvGoods.AutoGenerateColumns=false;
dgvGoods.DataSource=datas;
}
綁定一下下拉選擇的值ValueMember==ID
DisplayMember==GTName
private bool ValidateAdd(){
//添加前的驗證方法
if(!txtGCode.ReadOnly && string.IsNullOrEmpty(txtGCode.Text.Trim())){
Message.Show("商品編號不能為空")
txtGCode.Focus();
return false;
}
if((int)(cbosup.SelectedValue==0){
Message.Show("請廳供應商")
cbosup.Focus();
return false;
}
if(nudBuyPrice.Value==0){
Message.Show("進貨價不能為0")
nudBuyPrice.Focus();
return false;
}
return true;
}
private void SetGoodsTabForForm(GoodsTab goods){
goods.GCode = txtGCode.Text.Trim();
goods.GName = txtGName.Text.Trim();
}
//清空方法
private void Clear(){
textGCode.Clear();
cboGoodType.SelectedIndex=0;
nudBuyingPrice.Value=0;
}
//添加按鈕方法
private void btnAdd_Click(object sender,EventArgs e){
if(ValidateAdd()){
Add();
btnSearch_Click(null,null);//添加成功后執行一下查詢
}
}
添加右鍵菜單
private GoodsTab GetGoodsForIndex(int index){
return ((List<GoodsTab>)dgvGoods.DataSource)[index];
}
private void changeOption(){
if(btnAdd.Text=="增加"){
btnAdd.Text="修改";
txtGCode.ReadOnly="true";
btnCleaar.Text="取消";
}
}
private void tsmiUpdate_Click(object sender,EventArgs e){
if(dgvGoods.CurrentRow == null) return ;
changeOption();
var goods = GetGoodsForIndex(dgvGoods.CurrentRow.index);
btnAdd.Tag = goods;
SetFormForGoodsTab(goods);
}
private void tsmiDelete_Click(object sender,EventArgs e){
if(dgvGoods.CurrentRow == null) return ;
if(MessageBox.Show("是否刪除嗎?","提示",MessageBoxButtons.OkCancel,MessageBoxIcon.Warning)==DialogResult.OK){
string message = "刪除成功";
Service.Delete(GetGoodsForIndex(dgvGoods.CurrentRow.Index).ID,ref message);
}
}