ComboBox控件的数据绑定
来源:互联网 发布:视频打马赛克软件 mac 编辑:程序博客网 时间:2024/05/14 10:45
ComboBox控件是一个下拉组合控件,该控件可以让用户输入或下拉选择内容。在使用的时候经常只是绑定数据表中的其中一列或把其中一列的值添加到Items中,如:name 。但在查询或使用的时候有可能需要name 所对应的ID,然而这时朋友们可能还要再用一条查询语句将name对应的ID取出来。下面我说一下我的做法提供大家参考一下。
首先新建一个windows项目,然后将ComboBox控件拖放到Form1中取名comboBox1.
下面是Form1类中的代码:
方法1:
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;
namespace C15Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
InitComboBox(comboBox1);
}
private void InitComboBox(ComboBox combobox)
{
combobox.DrawMode = DrawMode.Normal;//设置绘制方式
combobox.DropDownHeight = 60;
combobox.DropDownStyle = ComboBoxStyle.DropDown;
combobox.FlatStyle = FlatStyle.Flat; //设置外观
/*一般应用
combobox.Items.Add("hello");
combobox.Items.Add("word");
*/
数据绑定方式
DataTable sourcettable = new DataTable();
sourcettable.Columns.Add("ID");
sourcettable.Columns.Add("Code");
sourcettable.Columns.Add("Name");
sourcettable.Columns.Add("Telephone");
sourcettable.Columns.Add("Address");
DataRow dr1 = sourcettable.NewRow ();
dr1["ID"] = "1";
dr1["Code"] = "001";
dr1["Name"] = "张三";
dr1["Telephone"] = "12345";
dr1["Address"] = "中国广东*******";
sourcettable.Rows.Add(dr1);
DataRow dr2 = sourcettable.NewRow ();
dr2["ID"] = "2";
dr2["Code"] = "002";
dr2["Name"] = "李四";
dr2["Telephone"] = "123456";
dr2["Address"] = "中国北京*******";
sourcettable.Rows.Add(dr2);
combobox.DataSource = sourcettable;//设置数据源
combobox.DisplayMember = "Name";//设置显示列
combobox.ValueMember = "ID";//设置实际值
combobox.SelectedIndexChanged += new EventHandler(combobox_SelectedIndexChanged);
}
void combobox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox combobox = sender as ComboBox;
if (combobox != null)
{
MessageBox.Show(combobox.SelectedValue.ToString());
}
}
}
方法2:
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;
namespace C15Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
InitComboBox(comboBox1);
}
private void InitComboBox(ComboBox combobox)
{
combobox.DrawMode = DrawMode.Normal;//设置绘制方式
combobox.DropDownHeight = 60;
combobox.DropDownStyle = ComboBoxStyle.DropDown;
combobox.FlatStyle = FlatStyle.Flat; //设置外观
/*一般应用
combobox.Items.Add("hello");
combobox.Items.Add("word");
*/
//特殊应用
Employee employee1 = new Employee();
employee1.ID = "1";
employee1.Code = "001";
employee1.Name = "张三";
employee1.Telephone = "022123456";
employee1.Address = "中国天津××××";
Employee employee2 = new Employee();
employee2.ID = "2";
employee2.Code = "002";
employee2.Name = "李四";
employee2.Telephone = "010123456";
employee2.Address = "中国北京*******";
//将Employee对象添加到combobox控件中
combobox.Items.Add(employee1);
combobox.Items.Add (employee2 );
//将combobox控件的selectedindexchanged 事件拖管到combobox_SelectedIndexChanged事件中
combobox.SelectedIndexChanged += new EventHandler(combobox_SelectedIndexChanged);
}
//当控件选择的项发生变化时执行代码
void combobox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox combobox = sender as ComboBox;
if (combobox != null)
{
Employee employee = (Employee)combobox.SelectedItem;//获取当前选中项
if (employee != null)
{
MessageBox.Show(string.Format("ID:{0},Code:{1},Name:{2},Telephone:{3},Address:{4}", employee.ID, employee.Code, employee.Name, employee.Telephone, employee.Address));
}
}
}
}
- ComboBox控件的数据绑定
- ComboBox控件的数据绑定
- Combobox 控件绑定数据
- flex中combobox控件的数据绑定
- ComboBox数据的绑定
- ComboBox 控件绑定数据和获取选中的数据
- vs2005标准控件GridView中Combobox的数据绑定
- ComboBox、TextBox和labelk控件的绑定数据用法
- ComboBox控件的绑定问题
- 将ComboBox 控件绑定到数据
- winform技巧—combobox控件数据绑定
- wpf ComboBox 控件利用DataSet数据绑定
- WinForm 中 comboBox控件之数据绑定
- Combobox数据绑定的问题
- ComboBox控件的绑定与获取值
- C# ComboBox控件 与 DataTable 的绑定
- WinFrm下DataGridView控件和Combobox控件绑定数据
- C# 绑定ComBobox控件
- replace
- BUG记录的几个经验要点
- 精确统计程序运行时间
- 西方同行眼中的东方程序员
- 【Matlab】Huffman编码如何实现数据压缩
- ComboBox控件的数据绑定
- 网络语言的诞生和去路
- 呼叫中心构想
- 联系我
- 雷万云谈云计算:就算是泡沫,也要踩破了走过去
- dede伪静态
- gdb
- rand()和srand()函数
- (vc base)除虫记之十二:费解的NTDLL断点