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);
}
}
- combobox控件绑定键值对存入数据库
- c# ComboBox 控件 用数据库填充 绑定
- HashMap键值对存入解析
- delphi中combobox键值对
- delphi中combobox键值对
- C# 绑定ComBobox控件
- ComboBox控件绑定数据源
- winform combobox控件绑定
- Combobox 控件绑定数据
- comboBox绑定数据库操作
- comboBox绑定数据库操作
- wpf:combobox绑定数据库
- .Net 在WinForm控件ComboBox的Items中使用非数据关联的键值对数据
- Spinner绑定键值对信息
- WinForm中让ComboBox Add 键值对
- delphi中combobox键值对addobject
- ComboBox控件的绑定问题
- ComboBox控件的数据绑定
- node.js express + mysql(一)
- (十九)在现有项目中集成ReactNaitve
- go实现一个简单的游戏服务器框架(lotou)多节点
- Apk反编译&回编译(一)
- [JAVA] Thinking In Java细节复习(一)
- combobox控件绑定键值对存入数据库
- 【Oracle】作为oracle新手学习的一些常用操作
- SQL优化:化解表关联的多对多join
- 单例模式中为什么用枚举更好
- Java判断文件编码工具类
- VBA保存Excel文件
- 每日一结:Android View随手指移动 CoordinatorLayout
- apache和nginx区分设备
- 让后端tomcat日志获取真实的IP,而不是nginx 服务器的IP