C#winfrom商品管理系统
=================超市管理系统--商品管理========================================
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);
		
		}
	}
                    
                
                
            
        
浙公网安备 33010602011771号