WinForm 中 comboBox控件数据绑定
一、IList
现在我们直接创建一个List集合,然后绑定
IList<string> list = new List<string>();
list.Add("111111");
list.Add("222222");
list.Add("333333");
list.Add("444444");
comboBox1.DataSource = 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才可以完成绑定
Dictionary<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";
三、数据集
//数据集绑定
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.SelectedValue
WinForm 中 comboBox控件数据绑定的更多相关文章
- [C#]WinForm 中 comboBox控件之数据绑定
[C#]WinForm 中 comboBox控件之数据绑定 一.IList 现在我们直接创建一个List集合,然后绑定 IList<string> list = new List<s ...
- winform中comboBox控件加默认选项的问题
winform程序设计中,label,TextBox,ComboBox等几个控件几乎是用得最多的,在设计中经常会遇到一些小问题,如:comboBox控件绑定了数据源之后,如何设置默认值? combob ...
- WinForm 中 comboBox控件之数据绑定
一.IList 现在我们直接创建一个List集合,然后绑定 1 IList<string> list = new List<string>(); 2 list.Add(&quo ...
- winform中ComboBox控件的简单使用
在开发winform中用到了ComboBox,但是发现和asp.net中的DropDownList差别比我想象中的大. 给ComboBox添加数据总结的有两种方法(绑定数据库在这里不说): 第一种方法 ...
- Winform中checklistbox控件的常用方法
Winform中checklistbox控件的常用方法最近用到checklistbox控件,在使用其过程中,收集了其相关的代码段1.添加项checkedListBox1.Items.Add(" ...
- Winform中Treeview控件失去焦点,将选择的节点设置为高亮显示 (2012-07-16 13:47:07)转载▼
Winform中Treeview控件失去焦点,将选择的节点设置为高亮显示 (2012-07-16 13:47:07)转载▼标签: winform treeview drawnode Treeview控 ...
- WinForm编程时窗体设计器中ComboBox控件大小的设置
问题描述: 在VS中的窗体设计器中拖放一个ComboBox控件后想调整控件的大小.发现在控件上用鼠标只能拖动宽度(Width)无法拖动(Height). 解决过程: 1.控件无法拖动,就在属性窗口中设 ...
- C#中combobox 控件属性、事件、方法
一 .combobox 属性.事件.方法公共属性 名称 说明 AccessibilityObject 获取分配给该控件的 AccessibleObject. AccessibleDefaultActi ...
- Winform中TextBox控件开启自动提示补全功能
问题:Winform开发中,有一个TextBox控件用以输入姓名,现希望在输入名字时能够自动提示所有可能的名字. 解答:winform中的TextBox控件含有如下三个属性: ① AutoComp ...
随机推荐
- Django 应用 静态文件配置
Django 应用 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- GUI常用对象介绍3
%text hf = axes; ht = text(,,'示例'); get(ht); %公式 并且设置位置坐标 (积分符号) text('String','\int_0^x dF(x)','Pos ...
- 一位前辈的博客,收获颇丰,包括Android、Java、linux、前端、大数据、网络安全等等
https://www.cnblogs.com/lr393993507/ 魔流剑
- 怎样从外网访问内网Nginx?
本地安装了一个Nginx,只能在局域网内访问,怎样从外网也能访问到本地的Nginx呢?本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Nginx 默认安装的Nginx端口是80. 实现步 ...
- Java中的ASCII码与Unicode码
先上代码 后续更新 public class Unicode { public static void main(String[] args) { char ch1 = 'c'; char ch2 = ...
- 2017第十三届湖南省省赛B - Simplified Blackjack CSU - 1998
在一次聚会上,Bob打算和Alice一起玩Blackjack游戏,但Alice平时很少玩扑克类游戏,Bob觉得跟Alice解释清楚Blackjack的规则有点困难,于是Bob决定和Alice玩一次简化 ...
- Piggy-Bank HDU - 1114
Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. ...
- django 把函数装饰器变为方法装饰器
暗暗啊
- Solr基本操作
/update 使用/update进行索引维护,进入Solr管理界面SolrCore下的Document下: 我们进行更新操作可以用json和xml多种格式,这里以xml格式为例说明.先来看看界面上的 ...
- css3-动画(animation)
css3-动画(animation): 具有以下属性: 1.animation-name 自定义动画名称 2.animation-duration 动画指定需要多少秒或毫秒完成,默认值是0; 3.an ...