分类管理各项商品 ,入门级示例源码,适合新手附加上数据库后,修改 DBHelper.cs中的 数据库 Pwd 密码 即可,登陆账号 jbit 密码 123
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Data.SqlClient;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace SuperMarket{ public partial class frmProductlist : Form { public frmProductlist() { InitializeComponent(); } DataSet ds = new DataSet();//创建数据集 #region 窗体加载事件 //窗体加载事件 private void frmProductlist_Load(object sender, EventArgs e) { Bind(); } #endregion #region 初始化 public void Bind() { this.dgvspxs.AutoGenerateColumns = false; string sql = "select c.*,s.SortName from Commodity c,CommoditySort s where c.SortID = s.SortID"; SqlDataAdapter adapter = new SqlDataAdapter(sql, DBHelper.ConnString); if (ds.Tables["Commodity"] != null) { ds.Tables["Commodity"].Clear(); } adapter.Fill(ds, "Commodity"); this.dgvspxs.DataSource = ds.Tables["Commodity"]; } #endregion #region 单击退出 private void tstc_Click(object sender, EventArgs e) { this.Close(); } #endregion #region 选中后事件 //选中后事件 private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { Cood(); } #endregion #region 筛选 //筛选 public void Cood() { DataView dv = new DataView(ds.Tables["Commodity"]);//获取深度 TreeNode tv = this.tvree.SelectedNode; if (tv.Level != 0) { dv.RowFilter = "IsDiscount = '" tv.Tag "'"; } this.dgvspxs.DataSource = dv; } #endregion #region 单击增加 private void tszj_Click(object sender, EventArgs e) { frmCompile ws = new frmCompile(); ws.ShowDialog(); this.Bind(); } #endregion #region 单击删除 private void tssc_Click(object sender, EventArgs e) { int num = Convert.ToInt32(this.dgvspxs.CurrentRow.Cells[0].Value); DialogResult result = MessageBox.Show("确定要删除吗", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); if (result == DialogResult.OK) { SqlConnection conn = new SqlConnection(DBHelper.ConnString); conn.Open(); string sql = "delete Commodity where CommodityID=" num; SqlCommand comm = new SqlCommand(sql, conn); int sum = comm.ExecuteNonQuery(); if (sum >= 1) { MessageBox.Show("删除成功"); Bind(); } else { MessageBox.Show("删除失败"); } conn.Close(); } } #endregion #region 单击修改 private void tsxg_Click(object sender, EventArgs e) { frmCompile qs = new frmCompile(); qs.num = Convert.ToInt32(this.dgvspxs.SelectedCells[0].Value); qs.ShowDialog(); this.Bind(); } #endregion }}
评论