ComBox绑定数据源

piaoyao888 2008-05-23 10:01:55
请问一下ComBox如何绑定数据源,将value属性和Text属性一块绑定!
...全文
481 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
renlei413326889 2008-07-11
  • 打赏
  • 举报
回复
请问
this.ComboBox1.DisplayMember = "Name";//字段名
this.ComboBox1.ValueMember = "userId";//字段名
---两个字段名怎么不相同?
分别表示什么?
有实例么?谢谢!
marvelstack 2008-05-23
  • 打赏
  • 举报
回复
mboBox1.DataSource = myDataSet ;//数据集
ComboBox1.DisplayMember = "person.userId" ;//表名加字段名
ComboBox1.ValueMember = "person.username" ;

this.ComboBox1.DataSource = myDataTable;//数据表
this.ComboBox1.DisplayMember = "Name";//字段名
this.ComboBox1.ValueMember = "userId";//字段名
funlove999 2008-05-23
  • 打赏
  • 举报
回复

using System;
using System.Windows.Forms ;
using System.Drawing ;
using System.Collections ;


namespace MyListControlSample
{

public class USState
{
private string myShortName ;
private string myLongName ;

public USState(string strLongName, string strShortName)
{

this.myShortName = strShortName;
this.myLongName = strLongName;
}

public string ShortName
{
get
{
return myShortName;
}
}

public string LongName
{

get
{
return myLongName ;
}
}

public override string ToString()
{
return this.ShortName + " - " + this.LongName;
}
}

public class ListBoxSample3:Form
{
private ListBox ListBox1 = new ListBox();
private TextBox textBox1 = new TextBox() ;

[STAThread]
static void Main()
{
Application.Run(new ListBoxSample3()) ;
}

public ListBoxSample3()
{

this.ClientSize = new Size(292, 181) ;
this.Text = "ListBox Sample3" ;

ListBox1.Location = new Point(24, 16) ;
ListBox1.Name = "ListBox1" ;
ListBox1.Size = new Size(232, 130) ;



textBox1.Location = new Point(24, 160) ;
textBox1.Name = "textBox1" ;
textBox1.Size = new Size(240, 24) ;
this.Controls.AddRange(new Control[] {ListBox1, textBox1}) ;

// Populates the list box using DataSource.
// DisplayMember is used to display just the long name of each state.
ArrayList USStates = new ArrayList() ;
USStates.Add(new USState("Alabama", "AL"));
USStates.Add(new USState("Washington", "WA")) ;
USStates.Add(new USState("West Virginia", "WV"));
USStates.Add(new USState("Wisconsin", "WI")) ;
USStates.Add(new USState("Wyoming", "WY"));

ListBox1.SelectedValueChanged += new EventHandler(ListBox1_SelectedValueChanged);
ListBox1.DataSource = USStates ;
ListBox1.DisplayMember = "LongName" ;
ListBox1.ValueMember = "ShortName" ;

}
private void InitializeComponent()
{

}

private void ListBox1_SelectedValueChanged(object sender, EventArgs e)
{
if (ListBox1.SelectedIndex != -1)
textBox1.Text = ListBox1.SelectedValue.ToString();
}
}
}

摘自MSDN
csharp_start 2008-05-23
  • 打赏
  • 举报
回复
给combobox设置datasource,displaymember,valuemember
piaoyao888 2008-05-23
  • 打赏
  • 举报
回复
自己先顶一下!

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧