C#窗口程序ComboBox怎么和List<string>数据绑定

 我来答
021_52739999
2017-11-01 · 超过28用户采纳过TA的回答
知道答主
回答量:310
采纳率:28%
帮助的人:36.2万
展开全部
主要利用comboBox1.DisplayMember comboBox1.ValueMember两属性
、绑定数据源情况
Form1_Load()函数添加代码:
ArrayList lst = new ArrayList();     //列表
lst.Add(new Vendor("a_geshi03", "20")); //列表增加象
lst.Add(new Vendor("a_geshi04", "30"));
comboBox1.Items.Clear(); //清空combobox
comboBox1.DataSource = lst; //lst列表绑定combobx
comboBox1.DisplayMember ="Strtemname";// 指定显示数据项
comboBox1.ValueMember = "Strindex"; //指定comboBox1.SelectedValue返数据项
3.其Vendor自定义类:
  class Vendor
{
private string strtemname;
private string strindex;
public Vendor(string itemname,string index)
{
this.strtemname = itemname;
this.strindex = index;
}
public string Strtemname
{
get { return strtemname; }
set { strtemname = value; }
}
public string Strindex
{
get { return strindex; }
set { strindex = value; }
}
}

二、绑定数据源情况:
comboBox.DataSource = list;//绑定表
comboBox.DisplayMember = "name";//显示数据
comboBox.ValueMember = "id";//台数据
或者直接界面手绑定即:
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
魏信1359111540
2017-11-01 · 超过35用户采纳过TA的回答
知道答主
回答量:115
采纳率:63%
帮助的人:43.9万
展开全部
// 定义数据类型
class demoInfo
{
public int ID{get;set;}
public stringi Value{set;get;}
}

// 绑定数据combobox
List lst = new List();
lst.Add(new demoInfo(){ID=1,Value="阿杜"});
comboBox.ValueMember = "Value";
comboBox.DisplayMember = "ID";
comboBox.DataSource = lstSearchFilesInfo;

// combobox注册SelectedIndexChanged事件
comboBox_SelectedIndexChanged 函数:
if(comboBox.SelectedValue!=null)
{
textbox.text = comboBox.SelectedValue.tostring();
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式