combobox控件绑定键值对存入数据库

来源:互联网 发布:中央电大网络教育绵阳 编辑:程序博客网 时间:2024/05/18 10:45

问题描述:客户信息管理系统中客户分类包含大客户、一般客户、小客户,如何做到新增客户的时候在combobox控件选择客户类型后存入数据库的是对应的数字

Part1-定义一个ComboBoxHelper类

 public class ComboBoxHelper
    {
        /// <summary>
        /// 列表项
        /// </summary>
        public class ListItem
        {
            public string Text { get; set; }
            public string Value { get; set; }
            public ListItem(string strText, string strValue)
            {
                this.Text = strText;
                this.Value = strValue;
            }
            public override string ToString()
            {
                return this.Text;
            }
            /// <summary>
            /// 根据ListItem中的Value找到特定的ListItem(仅在ComboBox的Item都为ListItem时有效)
            /// </summary>
            /// <param name="cmb">要查找的ComboBox</param>
            /// <param name="strValue">要查找ListItem的Value</param>
            /// <returns>返回传入的ComboBox中符合条件的第一个ListItem,如果没有找到则返回null.</returns>
            public static ListItem FindByValue(ComboBox cmb, string strValue)
            {
                foreach (ListItem li in cmb.Items)
                {
                    if (li.Value == strValue)
                    {
                        return li;
                    }
                }
                return null;
            }
            /// <summary>
            /// 根据ListItem中的Text找到特定的ListItem(仅在ComboBox的Item都为ListItem时有效)
            /// </summary>
            /// <param name="cmb">要查找的ComboBox</param>
            /// <param name="strValue">要查找ListItem的Text</param>
            /// <returns>返回传入的ComboBox中符合条件的第一个ListItem,如果没有找到则返回null.</returns>
            public static ListItem FindByText(ComboBox cmb, string strText)
            {
                foreach (ListItem li in cmb.Items)
                {
                    if (li.Text == strText)
                    {
                        return li;
                    }
                }
                return null;
            }

        }

    }

 

Part2-新增客户信息的表示层

public partial class formAdd : Form
    {
        public formAdd()
        {
            InitializeComponent();
            List<WSJXC.Common.ComboBoxHelper.ListItem> items = new List<WSJXC.Common.ComboBoxHelper.ListItem>();//完成combobox控件与键值对的绑定,其中combobox控件的名称为txtType
            items.Add(new WSJXC.Common.ComboBoxHelper.ListItem("大客户", "1"));
            items.Add(new WSJXC.Common.ComboBoxHelper.ListItem("一般客户", "2"));
            items.Add(new WSJXC.Common.ComboBoxHelper.ListItem("小客户", "3"));
            txtType.DisplayMember = "Text";        //显示
            txtType.ValueMember = "Value";        //值
            txtType.DataSource = items;  
        }
        private void btnAddsubmit_Click(object sender, EventArgs e)
        {
           
            if (txtAddname.Text == "")
            {
                MessageBox.Show("客户名称必填", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (txtType.SelectedIndex == -1)
            {
                MessageBox.Show("客户分类必选", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if(txtAddphone.Text == "")
            {
                MessageBox.Show("客户电话必填", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                WSJXC.Common.ComboBoxHelper.ListItem selectedItem = (WSJXC.Common.ComboBoxHelper.ListItem)txtType.SelectedItem;
                string value = selectedItem.Value;    //客户类型汉字字符串对应的数字字符串
                string text = selectedItem.Text;    //combobox控件显示的文字
                string pinyin = txtAddname.Text.Trim();//名称全拼
                Customer customer = new Customer();
                customer.Name = txtAddname.Text.Trim();
                customer.Type = int.Parse(value.ToString());//数字字符串存入数据库
                customer.Phone = txtAddphone.Text.Trim();
                customer.Email = txtEmail.Text.Trim();
                customer.QQ = txtQQ.Text.Trim();
                customer.Birthday = DateTime.Parse(txtAddbirthday.Text.Trim());
                customer.IDCard = txtAddidcard.Text.Trim().ToString();
                customer.Address = txtAddaddress.Text.Trim();
                customer.Remark = richTxtAddremark.Text.Trim();
                customer.Pinyin = WS.CRM.AppForm.NameHelper.ChsString2Spell(pinyin);
                customer.PinyinAbbr = WS.CRM.AppForm.NameHelper.GetHeadOfChs(pinyin);

                CustomerBLL bll = new CustomerBLL();

                bool success = bll.Submit(customer);

                if (success == true)
                {
                    MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("添加失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

 

}

Part3-业务逻辑层

public class CustomerBLL
    {
        CustomerDAL dal = new CustomerDAL();

       public bool Submit(Customer customerP)
        {
            dal.InsertCustomer(customerP);
            return true;
        }

}

Part4-数据访问层

public class CustomerDAL//在数据访问层完成向数据库插入一条数据
    {

       public void InsertCustomer(Customer customer)
        {

            string sql = "insert into customer (name,type,email,qq,birthday,id_card,phone,address,remark,creationtime,pinyin,pinyin_abbr)values ('" + customer.Name + "'," +       customer.Type + ",'" + customer.Email + "','" + customer.QQ + "','" + customer.Birthday + "','" + customer.IDCard + "','" + customer.Phone + "','" + customer.Address + "','" + customer.Remark + "',datetime('now', 'localtime'),'" + customer.Pinyin + "','" + customer.PinyinAbbr + "')";
            SQLiteHelper.ExecuteNonQuery(sql, null);
        }

 

}
0 0
原创粉丝点击