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;
using System.Collections;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void bindDate()
{
ArrayList arrayList = new ArrayList();
string[] posMsg = { "1,a", "2,b" };
for (int i = 0; i < posMsg.Length; i++)
{
string[] oneMsg;
oneMsg = posMsg[i].Split(',');
arrayList.Add(new DictionaryEntry(oneMsg[0], oneMsg[1]));
}
comboBox1.DataSource = arrayList;
comboBox1.ValueMember = "Key";
comboBox1.DisplayMember = "Value";
}
private void Form1_Load(object sender, EventArgs e)
{
bindDate();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedValue != null)
{
label1.Text = comboBox1.SelectedValue.ToString();
label2.Text = comboBox1.Text;
}
}
}
}
ComboBox数据绑定
本文介绍了一个使用C#实现的简单示例,演示了如何通过数组列表和字典项为Windows窗体中的ComboBox控件提供数据源,并设置显示成员和值成员。
2万+

被折叠的 条评论
为什么被折叠?



