C__WinForm_中ComboBox数据绑定的问题
winform中的ListBox和ComboBox绑定数据用法实例

本例实现将集合数据绑定到ListBox和ComboBox控件,界面上显示某个属性的内容,代码如下:
复制代码 代码如下:
//... //自定义了Person类(有Name,Age,Heigth等属性)
List<Person> persons=new List<Person>(); persons.Add(new Person("WuMiao",18,175)); persons.Add(new Person("YeXinYv",20,170)); persons.Add(new Person("WuDong",18,175));
arraylist是接口list的实现类所以在使用过程中比较推荐使用list接口来实现arraylist在程序开发过程中应用非常广泛接下来脚本之家的小编给大家总结了arraylist的使用有需要的朋友可以参考下
winform中的 ListBox和 ComboBox绑定数据用法实例
本文实例讲述了winform中的ListBox和ComboBox绑定数据用法。分享给大家供大家参考。具体实现方法如下:
//ListBox控件实现 lb_PersonsList.DataSource=persons; //指定数据源 lb_PersonList.DisplayMember="Name"; //界面显示的是人的名字
//ComboBox控件实现 (与Le=persons; cmb_PersonList.DisplayMember="Name";
Winfrom中ListBox绑定List数据源更新问题

Winfrom中 ListBox绑定 List数据源更新问题
Winfrom中ListBox绑定List数据源,第一次可以成功,但后面List更新以后,ListBox并没有更新。
如果 ListBox的数据源 是 DataTable 是可以自动更新的,但若是 List<T> 时对数据的修改界面不会更新,使用 BindingSource 绑定就可以 了。 private void InitSample() { ListBox listControl = new ListBox(); List<Employee> listSource = new List<Employee>(); BindingSource bs = new BindingSource(); bs.DataSource = listSource;
this.Controls.Adห้องสมุดไป่ตู้(listControl); }
补充:使用BindingList亦可解决此问题!
listControl.DataSource = bs; listControl.DisplayMember = “Name”; listControl.ValueMember = “Id”;
// 事先绑定了,这时修改数据源会自动刷新界面显示 listSource.Add(new Employee(1, “Sam”)); listSource.Add(new Employee(2, “John”));
为comboBox控件添加数据绑定

为comboBox控件添加数据绑定(C#、sql server2008)
1.添加comboBox控件(列表框)
创建Form1窗口,点击工具箱中的ComboBox控件,直接拉到窗口中
2.点击ComboBox控件,出现三角号,点击三角号,勾上使用数据绑定项,点
击数据源右边的下拉按纽,在弹出的窗口中点击“添加项目数据源”,弹出新窗口(数据源配置向导)
3.选择数据库,点击下一步
4.选择数据集,点击下一步
5.点击‘新建连接’按纽
Server),点击继续
7.在服务器名中输入数据库的服务器名,选择数据库,点击确定
8.回到选择数据连接窗口,点击下一步
9.选择要导入的表或视图中上列(只能选择一列),点击完成。
10.选择显示成员,值成员和选定值,完成。
Winform实现combox控件手动匹配查找,模糊查询功能

Winform实现combox控件⼿动匹配查找,模糊查询功能1.设置Combox属性: DropDownStyle:DropDown2.添加TextUpdate事件3.下列为Name = cb_material 的 combox 控件private void cb_material_TextUpdate(object sender, EventArgs e){string s = this.cb_material.Text; //获取cb_material控件输⼊内容List<string> strList = new List<string>(); //存放原始数据(可以是对象,字符串...)strList.AddRange(materials.ToArray()); // List<string> materialsList<string> strListNew = new List<string>();//清空comboboxthis.cb_material.Items.Clear();//遍历全部原始数据foreach (var item in strList){// 根据输⼊的值模糊查询,将符合条件的值存储到新strListNew的集合⾥⾯if (item.shape.Contains(this.cb_material.Text)){strListNew.Add(item);}}if (strListNew.Count >= 1) // 存在符合条件的内容{//将符合条件的内容加到combobox中this.cb_material.Items.AddRange(strListNew.ToArray());}else // 不存在符合条件时{// 下列代码为当查询不到符合的条件时新增⾃⾝输⼊的值// this.cb_material.Items.Add(this.cb_material.Text);}//设置光标位置,若不设置:光标位置始终保持在第⼀列,造成输⼊关键词的倒序排列this.cb_material.SelectionStart = this.cb_material.Text.Length; // 设置光标位置,若不设置:光标位置始终保持在第⼀列,造成输⼊关键词的倒序排列 Cursor = Cursors.Default; //保持⿏标指针原来状态,有时候⿏标指针会被下拉框覆盖,所以要进⾏⼀次设置this.cb_material.DroppedDown = true; // ⾃动弹出下拉框}。
Winform中多个ComboBox联动问题

Winform中多个ComboBox联动问题本⼈在窗体中添加了⼀个FlowLayoutPanel控件flpControlContain,为了能让控件⾃动排位(省了⾃⼰控制位置)代码如下://在窗体中添加⼀个按钮,每当该按钮点击⼀次,添加⼀个下拉框private void btnAddNew_Click(object sender, EventArgs e){this.ConditonCount++;//是⼀个私有字段,⽤于记录当前添加的是第⼏个,每添加⼀个⾃增1//添加列名下拉列表ComboBox cmb = new ComboBox(); = "列"+ConditonCount;//设置控件名称,⽤于在夫容器查询到该控cmb.Width = SELECTWIDTH;//设置宽度,该值是⼀个常量cmb.DataSource = this._columnslist;//绑定了⼀个字符串集合作为数据源cmb.Tag = ConditonCount;//设置标识,为第⼏个cmb.DropDownStyle = ComboBoxStyle.DropDownList;//设置下拉框下拉⽅式为只能选不能输⼊cmb.SelectedIndexChanged += Cmb_SelectedIndexChanged;//绑定列名下拉框选项修改事件,⽬前只在该事件中输出控件的Tagthis.flpControlContain.Controls.Add(cmb);//添加到FlowLayoutPanel}当多个ComboBox添加到FlowLayoutPanel后,我这⾥绑定的下拉框数据源都是⼀样的,就会出现当⼀个下拉框选中某⼀项之后,所有的下拉框都会去选中同⼀选项!但是下拉框选中项改变事件只触发⼀次于是弱弱的去贴吧问⼤神了~~得到的回答是:-------------------------------------------------------------cmb.DataSource = this._columnslist;尝试修改为cmb.DataSource = this._columnslist.ToList() ;----------------------------------------------------------最后试了下,成功了!问题就来了,是不是和绑定的是同⼀个数据源就关系,于是⾃⼰新建了⼀个窗体,⼿动添加两个ComboBox,但是给他们绑定的都是同⼀个List集合实验发现还是会联动(选中ComboBox1中的⼀项,ComboBox2中也会选中同⼀项)但是问题⼜来了:为什么绑定同⼀个数据源,会影响到控件的⾏为?可惜:贴吧⼤神没有告诉我于是结论:多个控件绑定同⼀个数据源会影响到控件的⾏为(⾄少ComboBox会),于是建议数据源是List时使⽤List.ToList()⽅法,返回⼀个新的List,如果是DataTable就使⽤DataTable.copy()复制⼀个给它,以避免因为绑定的是同⼀个数据,⽽使某⼀个控件的⾏为会影响到绑定了同数据源的其他控件学习了!膜拜⼤神。
WinForm中comboBox控件之数据绑定

WinForm中comboBox控件之数据绑定下⾯介绍三种对comboBox绑定的⽅式,分别是泛型中IList和Dictionary,还有数据集DataTable⼀、IList现在我们直接创建⼀个List集合,然后绑定View CodeIList<string> list = new List<string>();list.Add("111111");list.Add("222222");list.Add("333333");list.Add("444444");comboBox1.DataSource = list;执⾏后,我们会发现绑定成功,但是我们知道⼀般对于下拉框的绑定都会有⼀个值,⼀个显⽰的内容,这个时候我们可以创建⼀个类,把value和text都封装到这个类,作为list的类型public class Info{public string Id { get; set; }public string Name { get; set; }}private void bindCbox(){IList<Info> infoList = new List<Info>();Info info1 = new Info() { Id="1",Name="张三"};Info info2 = new Info() { Id="2",Name="李四"};Info info3 = new Info() { Id = "3",Name = "王五" };infoList.Add(info1);infoList.Add(info2);infoList.Add(info3);comboBox1.DataSource = infoList;comboBox1.ValueMember = "Id";comboBox1.DisplayMember = "Name";}这个时候我们就可以直接获得值和显⽰的内容了⼆、Dictionary这个有点特殊,不能直接绑定,需要借助类BindingSource才可以完成绑定View CodeDictionary<int, string> kvDictonary = new Dictionary<int, string>();kvDictonary.Add(1, "11111");kvDictonary.Add(2, "22222");kvDictonary.Add(3, "333333");BindingSource bs = new BindingSource();bs.DataSource = kvDictonary;comboBox1.DataSource = bs;comboBox1.ValueMember = "Key";comboBox1.DisplayMember = "Value";三、数据集这个⽐较常见,很简单View Code//数据集绑定private void BindCombox(){DataTable dt = new DataTable();DataColumn dc1 = new DataColumn("id");DataColumn dc2 = new DataColumn("name");dt.Columns.Add(dc1);dt.Columns.Add(dc2);DataRow dr1 = dt.NewRow();dr1["id"] = "1";dr1["name"] = "aaaaaa";DataRow dr2 = dt.NewRow();dr2["id"] = "2";dr2["name"] = "bbbbbb";dt.Rows.Add(dr1);dt.Rows.Add(dr2);comboBox1.DataSource = dt;comboBox1.ValueMember = "id";comboBox1.DisplayMember = "name";}注意:当我们触发combox的SelectedIndexChanged的事件后,我们在加载窗体的时候就会执⾏,这点我刚开始也和魅惑,导致容易出错,这点我们可以采取⼀些⽅法避免执⾏,⽐如可以定义⼀个变量fig=falseprivate void comboBox1_SelectedIndexChanged(object sender, EventArgs e){if(this.fig){string selectValue = this.cmbAddMember.SelectedValue.ToString();rtbaddMember.SelectedText = selectValue;}} 那么肯定想在加载窗体后,执⾏了,所以在加载窗体后我们还要把fig的值设为trueprivate void SetAutoMessage_Load(object sender, EventArgs e){loadCombox();loadMessageTemplet();fig= true;}。
C#WinForm_ComboBox数据绑定的问题

Visual Studio C#中的数据绑定五.复杂型组件的数据绑定:在上面的介绍中,了解到对复杂型组件的数据绑定是通过设定组件的某些属性来完成数据绑定的。
首先来介绍一下ComboBox组件的数据绑定.(1).ComboBox组件的数据绑定:在得到数据集后,只有设定好ComboBox组件的的三个属性就可以完成数据绑定了,这三个属性是:、"DisplayMember"、"ValueMember"。
其中"DataSource"是要显示的数据集,"DisplayMember"是ComboBox 组件显示的字段,"ValueMember"是实际使用值。
具体如下:ComboBox1.DataSource = myDataSet ;ComboBox1.DisplayMember = "person.xm" ;ComboBox1.ValueMember = "person.xm" ;注释:此时绑定是Access 2000数据库中"person"表的"xm"字段。
由此可以得到ComboBox组件数据绑定的源程序代码(Combo01.cs),本代码操作数据库是Access 2000:public class Form1 : Form{private ComboBox ComboBox1 ;private Button button1 ;private System.Data.DataSet myDataSet ;private ponentModel.Container components = null ;public Form1 ( ){file://打开数据链接,得到数据集GetConnect ( ) ;InitializeComponent ( ) ;}file://清除程序中使用过的资源protected override void Dispose ( bool disposing ){if ( disposing ){if ( components != null ){components.Dispose ( ) ;}}base.Dispose ( disposing ) ;}private void GetConnect ( ){file://创建一个OleDbConnectionstring strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ;OleDbConnection myConn = new OleDbConnection ( strCon ) ;string strCom = " SELECT * FROM person " ;file://创建一个DataSetmyDataSet = new DataSet ( ) ;myConn.Open ( ) ;file://用OleDbDataAdapter 得到一个数据集OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;file://把Dataset绑定person数据表myCommand.Fill ( myDataSet , "person" ) ;file://关闭此OleDbConnectionmyConn.Close ( ) ;}private void button1_Click ( object sender , System.EventArgs e ){ComboBox1.DataSource = myDataSet ;ComboBox1.DisplayMember = "person.xm" ;ComboBox1.ValueMember = "person.xm" ;}static void Main ( ){Application.Run ( new Form1 ( ) ) ;}}图03:对ComboBox组件数据绑定的程序界面得到了ComboBox组件对本地数据库的数据绑定程序,也就十分方便的得到ComboBox组件绑定Sql Server 2000源程序代码(Combox02.cs)具体如下:public class Form1 : Form{private ComboBox ComboBox1 ;private Button button1 ;private System.Data.DataSet myDataSet ;private ponentModel.Container components = null ;public Form1 ( ){file://打开数据链接,得到数据集GetConnect ( ) ;InitializeComponent ( ) ;}file://清除程序中使用过的资源protected override void Dispose ( bool disposing ){if ( disposing ){if ( components != null ){components.Dispose ( ) ;}}base.Dispose ( disposing ) ;}private void GetConnect ( ){// 设定数据连接字符串,此字符串的意思是打开Sql server数据库,服务器名称为server1,数据库为data1 string strCon = "Provider = SQLOLEDB.1 ; Persist Security Info = False ; User ID = sa ; Initial Catalog = data1 ; Data Source = server1 " ;OleDbConnection myConn = new OleDbConnection ( strCon ) ;myConn.Open ( ) ;string strCom = " SELECT * FROM person " ;file://创建一个DataSetmyDataSet = new DataSet ( ) ;file://用OleDbDataAdapter 得到一个数据集OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;file://把Dataset绑定person数据表myCommand.Fill ( myDataSet , " person " ) ;file://关闭此OleDbConnectionmyConn.Close ( ) ;}private void button1_Click ( object sender , System.EventArgs e ){ComboBox1.DataSource = myDataSet ;ComboBox1.DisplayMember = "person.xm" ;ComboBox1.ValueMember = "person.xm" ;}static void Main ( ){Application.Run ( new Form1 ( ) ) ;}}C# WinForm 中ComboBox数据绑定的问题2009-12-29 09:24怎样让WinForm中的ComboBox显示表中的一个字段,同时又绑定另一个字段?在Web中的ComboBox这样写可以绑定两个值:boBox1.DataTextField="B000602";//显示中文,方便用户选择boBox1.DataValueField="B000601";//绑定与选择对应的另一个值boBox1.DataBind();但是在WinForm程序中该怎么写啊?0******************************************************************DataSet ds = new DataSet();//这个DataSet是你从数据库里取出来的值string[] arr = new string[ds.Tables[0].Rows.Count];for (int i = 0; i < arr.Length; i++){arr[i] = ds.Tables[0].Rows[i][2].ToString();}textBox1.AutoCompleteCustomSource.AddRange(arr);textBox1.AutoCompleteSource =AutoCompleteSource.CustomSource;textBox1.AutoCompleteMode =AutoCompleteMode.SuggestAppend;1.******************************************************************* 假设combobox绑定的列表为DataSet2的ListTable表(含有ListID, ListName字段),需要绑定的记录字段为DataSet1的Table1表的ListID字段combobox.DataSource = dataset2.Tables["ListTable"]; combobox.DisplayMember = "ListName";combobox.ValueMember = "ListID";combobox.DataBindings.Add("SelectedValue", dataset1, "Table1.List ID");2.***************************************************************** //dt为数据表,ID,Name为dt的两个字段:comboBox1.DataSource = dt ;comboBox1.ValueMember ="ID";comboBox1.DisplayMember ="Name";3.******************************************************************SqlConnection con = new SqlConnection("server=192.168.2.198;uid=sa;pwd=sa;database=northwind"); SqlCommand cmd = con.CreateCommand();mandText = "Select * from Customers where countr y='USA'";SqlDataAdapter adp = new SqlDataAdapter();adp.SelectCommand = cmd;DataSet ds = new DataSet();adp.Fill(ds, "Customers");comboBox1.DataSource = ds.Tables["Customers"];comboBox1.DisplayMember = "CompanyName";comboBox1.ValueMember = "CompanyName";++++++++++++++++或者++++++++++++++++++++++SqlConnection con = new SqlConnection("server=192.168.2.198;uid=sa;pwd=sa;database=northwind"); SqlCommand cmd = con.CreateCommand();mandText = "Select * from Customers where countr y='USA'";SqlDataAdapter adp = new SqlDataAdapter();adp.SelectCommand = cmd;DataSet ds = new DataSet();adp.Fill(ds, "Customers");comboBox1.DataSource = ds;comboBox1.DisplayMember = "panyName";comboBox1.ValueMember = "panyName";++++++++++++往DataGrid里添加下拉列表++++++++++++ DataGridTableStyle dgt = new DataGridTableStyle();dgt.MappingName = "test";DataGridTextBoxColumn dgtbc = new DataGridTextBoxColumn(); dgtbc.MappingName = "name";dgtbc.HeaderText= "name";ComboBox cmbFunctionArea = new ComboBox(); cmbFunctionArea.DataSource = DtGeneral;cmbFunctionArea.DisplayMember = "name"; cmbFunctionArea.ValueMember = "value";cmbFunctionArea.Cursor = Cursors.Arrow;cmbFunctionArea.DropDownStyle= ComboBoxStyle.DropDownList;cmbFunctionArea.Dock = DockStyle.Fill;dgtbc.TextBox.Controls.Add(cmbFunctionArea);dgt.GridColumnStyles.Add(dgtbc);cmbFunctionArea.SelectionChangeCommitted +=new EventHandler(cmbFunctionArea_SelectionChangeCommitted);+++++++++++++修改++++++++++++++++private void cmbFunctionArea_SelectionChangeCommitted(object se nder, EventArgs e){((DataTable)dataGrid1.DataSource).Rows[dataGrid1.CurrentRowIndex][0] = ((ComboBox)sender).Text.ToString();dataGrid1.SetDataBinding(DtGeneral,null);}4.******************************************************************* *****DataBindings是一般控件所具有的,是绑定数据源的某一个字段combobox.DataBindings.Add("要绑定控件的属性如下拉框的SelectedValue\Text", 数据源如dataset1, "导航路径如Table1.ListID");但是,DataBindings只能绑定一个字段,而绑定多个字段时典型的如列表控件Combobox、ListBox控件,需要键值对,这时就需要指定DataSource(实现IList接口就行),然后指定ValueMember、DisplayMember 。
C#WinForm复习题6

说明:针对以下题目请选择正确的答案(每道题目有一个或多个正确的答案)。
针对每一道题目,所有答案都选对,则该题得分。
所选答案错误或不能选出所有正确答案,则该题不得分。
1在Winforms中,有一个类名为Form的窗体,下列代码能够正确把该窗体显示为模式对话框的是()。
(选择一项)A) Form f=new Form(); f.Show(); f.Modal=true;B) Form f=new Form(Dialog);C) Form f=new Form(); f.Activate();D) Form f=new Form(); f.ShowDialog();2在Winform应用程序中,可以通过以下()方法使一个窗体成为MDI窗体。
(选择一项)A) 改变窗体的标题信息B) 在工程的选项中设置启动窗体C) 设置窗体的IsMdiContainer属性D) 设置窗体的ImeMode属性3在WinForms中,下列代码的运行结果是()。
(选择一项)DataSet ds = new DataSet(“Books”);DataTable dt = new DataTable(“Student”);Console.WriteLine(ds.DataSetName);A) Books B) Student C) ds D) ds.DataSetName 4下列说法正确的是()。
(选择一项)A) 要把用户对数据的修改更新到数据库中,可以使用DataSet的Update方法B) 调用DataAdapter的Fill方法将数据填充到DataSet数据集之前,必须设置SelectCommand属性C) DataSet数据集只能位于服务器端,不能把DataSet数据集传输到客户端D) 数据适配器是数据库和数据集的一个连接桥梁,因此只能有一个DataAdapter与一个DataSet一起使用5VB6.0和.NET中都有计时器控件,在VB中,计时器控件在其Timer事件中进行编码,在.net中,计时器控件提供触发的是()事件。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Visual C#中的数据绑定2002-02-26 15:49作者:阿虎出处:yesky责任编辑:方舟五.复杂型组件的数据绑定:在上面的介绍中,了解到对复杂型组件的数据绑定是通过设定组件的某些属性来完成数据绑定的。
首先来介绍一下ComboBox组件的数据绑定.(1).ComboBox组件的数据绑定:在得到数据集后,只有设定好ComboBox组件的的三个属性就可以完成数据绑定了,这三个属性是:、"DisplayMember"、"ValueMember"。
其中"DataSource"是要显示的数据集,"DisplayMember"是ComboBox 组件显示的字段,"ValueMember"是实际使用值。
具体如下:ComboBox1.DataSource = myDataSet ;ComboBox1.DisplayMember = "person.xm" ;ComboBox1.ValueMember = "person.xm" ;注释:此时绑定是Access 2000数据库中"person"表的"xm"字段。
由此可以得到ComboBox组件数据绑定的源程序代码(Combo01.cs),本代码操作数据库是Access 2000:public class Form1 : Form{private ComboBox ComboBox1 ;private Button button1 ;private System.Data.DataSet myDataSet ;private ponentModel.Container components = null ;public Form1 ( ){file://打开数据链接,得到数据集GetConnect ( ) ;InitializeComponent ( ) ;}file://清除程序中使用过的资源protected override void Dispose ( bool disposing ){if ( disposing ){if ( components != null ){components.Dispose ( ) ;}}base.Dispose ( disposing ) ;}private void GetConnect ( ){file://创建一个OleDbConnectionstring strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ; OleDbConnection myConn = new OleDbConnection ( strCon ) ;string strCom = " SELECT * FROM person " ;file://创建一个DataSetmyDataSet = new DataSet ( ) ;myConn.Open ( ) ;file://用OleDbDataAdapter 得到一个数据集OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ; file://把Dataset绑定person数据表myCommand.Fill ( myDataSet , "person" ) ;file://关闭此OleDbConnectionmyConn.Close ( ) ;}private void button1_Click ( object sender , System.EventArgs e ){ComboBox1.DataSource = myDataSet ;ComboBox1.DisplayMember = "person.xm" ;ComboBox1.ValueMember = "person.xm" ;}static void Main ( ){Application.Run ( new Form1 ( ) ) ;}}图03:对ComboBox组件数据绑定的程序界面得到了ComboBox组件对本地数据库的数据绑定程序,也就十分方便的得到ComboBox组件绑定Sql Server 2000源程序代码(Combox02.cs)具体如下:public class Form1 : Form{private ComboBox ComboBox1 ;private Button button1 ;private System.Data.DataSet myDataSet ;private ponentModel.Container components = null ;public Form1 ( ){file://打开数据链接,得到数据集GetConnect ( ) ;InitializeComponent ( ) ;}file://清除程序中使用过的资源protected override void Dispose ( bool disposing ){if ( disposing ){if ( components != null ){components.Dispose ( ) ;}}base.Dispose ( disposing ) ;}private void GetConnect ( ){// 设定数据连接字符串,此字符串的意思是打开Sql server数据库,服务器名称为server1,数据库为data1 string strCon = "Provider = SQLOLEDB.1 ; Persist Security Info = False ; User ID = sa ; Initial Catalog = data1 ; Data Source = server1 " ;OleDbConnection myConn = new OleDbConnection ( strCon ) ;myConn.Open ( ) ;string strCom = " SELECT * FROM person " ;file://创建一个DataSetmyDataSet = new DataSet ( ) ;file://用OleDbDataAdapter 得到一个数据集OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;file://把Dataset绑定person数据表myCommand.Fill ( myDataSet , " person " ) ;file://关闭此OleDbConnectionmyConn.Close ( ) ;}private void button1_Click ( object sender , System.EventArgs e ){ComboBox1.DataSource = myDataSet ;ComboBox1.DisplayMember = "person.xm" ;ComboBox1.ValueMember = "person.xm" ;}static void Main ( ){Application.Run ( new Form1 ( ) ) ;}}C# WinForm 中ComboBox数据绑定的问题2009-12-29 09:24怎样让WinForm中的ComboBox显示表中的一个字段,同时又绑定另一个字段?在Web中的ComboBox这样写可以绑定两个值:boBox1.DataTextField="B000602";//显示中文,方便用户选择boBox1.DataValueField="B000601";//绑定与选择对应的另一个值boBox1.DataBind();但是在WinForm程序中该怎么写啊?0******************************************************************DataSet ds = new DataSet();//这个DataSet是你从数据库里取出来的值string[] arr = new string[ds.Tables[0].Rows.Count];for (int i = 0; i < arr.Length; i++){arr[i] = ds.Tables[0].Rows[i][2].ToString();}textBox1.AutoCompleteCustomSource.AddRange(arr);textBox1.AutoCompleteSource =AutoCompleteSource.CustomSource;textBox1.AutoCompleteMode =AutoCompleteMode.SuggestAppend;1.******************************************************************* 假设combobox绑定的列表为DataSet2的ListTable表(含有ListID, ListName字段),需要绑定的记录字段为DataSet1的Table1表的ListID字段combobox.DataSource = dataset2.Tables["ListTable"]; combobox.DisplayMember = "ListName";combobox.ValueMember = "ListID";combobox.DataBindings.Add("SelectedValue", dataset1, "Table1.List ID");2.***************************************************************** //dt为数据表,ID,Name为dt的两个字段:comboBox1.DataSource = dt ;comboBox1.ValueMember ="ID";comboBox1.DisplayMember ="Name";3.******************************************************************SqlConnection con = new SqlConnection("server=192.168.2.198;uid=sa;pwd=sa;database=northwind"); SqlCommand cmd = con.CreateCommand();mandText = "Select * from Customers where countr y='USA'";SqlDataAdapter adp = new SqlDataAdapter();adp.SelectCommand = cmd;DataSet ds = new DataSet();adp.Fill(ds, "Customers");comboBox1.DataSource = ds.Tables["Customers"];comboBox1.DisplayMember = "CompanyName";comboBox1.ValueMember = "CompanyName";++++++++++++++++或者++++++++++++++++++++++SqlConnection con = new SqlConnection("server=192.168.2.198;uid=sa;pwd=sa;database=northwind"); SqlCommand cmd = con.CreateCommand();mandText = "Select * from Customers where country='USA'";SqlDataAdapter adp = new SqlDataAdapter();adp.SelectCommand = cmd;DataSet ds = new DataSet();adp.Fill(ds, "Customers");comboBox1.DataSource = ds;comboBox1.DisplayMember = "panyName";comboBox1.ValueMember = "panyName";++++++++++++往DataGrid里添加下拉列表++++++++++++ DataGridTableStyle dgt = new DataGridTableStyle();dgt.MappingName = "test";DataGridTextBoxColumn dgtbc = new DataGridTextBoxColumn(); dgtbc.MappingName = "name";dgtbc.HeaderText= "name";ComboBox cmbFunctionArea = new ComboBox(); cmbFunctionArea.DataSource = DtGeneral;cmbFunctionArea.DisplayMember = "name"; cmbFunctionArea.ValueMember = "value";cmbFunctionArea.Cursor = Cursors.Arrow;cmbFunctionArea.DropDownStyle= ComboBoxStyle.DropDownList;cmbFunctionArea.Dock = DockStyle.Fill;dgtbc.TextBox.Controls.Add(cmbFunctionArea);dgt.GridColumnStyles.Add(dgtbc);cmbFunctionArea.SelectionChangeCommitted +=new EventHandler(cmbFunctionArea_SelectionChangeCommitted);+++++++++++++修改++++++++++++++++private void cmbFunctionArea_SelectionChangeCommitted(object se nder, EventArgs e){((DataTable)dataGrid1.DataSource).Rows[dataGrid1.CurrentRowIndex][0]= ((ComboBox)sender).Text.ToString();dataGrid1.SetDataBinding(DtGeneral,null);}4.******************************************************************* *****DataBindings是一般控件所具有的,是绑定数据源的某一个字段combobox.DataBindings.Add("要绑定控件的属性如下拉框的SelectedValue\Text", 数据源如dataset1, "导航路径如Table1.ListID");但是,DataBindings只能绑定一个字段,而绑定多个字段时典型的如列表控件Combobox、ListBox控件,需要键值对,这时就需要指定DataSource(实现IList接口就行),然后指定ValueMember、DisplayMember 。