+ -
当前位置:首页 → 问答吧 → comboBox如何从选中的数据中删除数据

comboBox如何从选中的数据中删除数据

时间:2011-12-14

来源:互联网



















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;
using System.Data.SqlClient;
namespace SuperMaket
{
  public partial class GoodsTypeForm : Form
  {
  public GoodsTypeForm()
  {
  InitializeComponent();
  }
  //添加数据
  private void btnAddGoodsType_Click(object sender, EventArgs e)
  {
  int index = 0;

  if (txtType.Text.Trim() != "")
  {
  try
  {
  string sql1 = string.Format("select count(*) from CommoditySort where SortName='{0}'", txtType.Text.Trim());
  DBHelp.connection.Open();
  SqlCommand comm = new SqlCommand(sql1, DBHelp.connection);
  index = (int)comm.ExecuteScalar();

  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.Message + "查询");
  }
  finally
  {
  DBHelp.connection.Close();
  try
  {
  if (index >= 1)
  {
  MessageBox.Show("你输入的商品类别已存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  }
  else
  {
  string sql = string.Format("insert into CommoditySort(SortName) values('{0}')", txtType.Text);
  DBHelp.connection.Open();
  SqlCommand command = new SqlCommand(sql, DBHelp.connection);
  int number = command.ExecuteNonQuery();
  if (number < 1)
  {
  MessageBox.Show("添加商品类别失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  }
  else
  {
  MessageBox.Show("添加商品类别成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

  }
  }
  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.Message + "插入商品项出错");
  }
  finally
  {
  DBHelp.connection.Close();
  }
  txtType.Text = "";
  cboType.Items.Clear();
  InputInit();

  }
  }
  else
  {
  MessageBox.Show("请输入要添加的商品类型", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  }
  }
  //数据加载
  public void InputInit()
  {
  try
  {
  string sql = "select * from CommoditySort";
  SqlCommand command = new SqlCommand(sql, DBHelp.connection);
  DBHelp.connection.Open();
  SqlDataReader reder = command.ExecuteReader();
  string sortname = "";
  while (reder.Read())
  {
  sortname = (string)reder[1];
  cboType.Items.Add(sortname);
  }
  reder.Close();
  }

  finally
  {
  DBHelp.connection.Close();
  }
  }

  private void GoodsTypeForm_Load(object sender, EventArgs e)
  {
  InputInit();

  }
  //删除
  private void btnGoodsDel_Click(object sender, EventArgs e)
  {
  //删除代码如何写
   
  }
  }
}

作者: gt0212   发布时间: 2011-12-14

...咋有花括号的聂.

作者: WallesCai   发布时间: 2011-12-14