[C#]WinForm 中 comboBox控件之数据绑定

一、IList

现在我们直接创建一个List集合,然后绑定

 IList<string> list = new List<string>();
list.Add("");
list.Add("");
list.Add("");
list.Add("");
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="",Name="张三"};
Info info2 = new Info() { Id="",Name="李四"};
Info info3 = new Info() { Id = "",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(, "");
kvDictonary.Add(, "");
kvDictonary.Add(, ""); 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"] = "";
dr1["name"] = "aaaaaa"; DataRow dr2 = dt.NewRow();
dr2["id"] = "";
dr2["name"] = "bbbbbb"; dt.Rows.Add(dr1);
dt.Rows.Add(dr2); comboBox1.DataSource = dt;
comboBox1.ValueMember = "id";
comboBox1.DisplayMember = "name";
}

注意:

当我们触发combox的SelectedIndexChanged的事件后,我们在加载窗体的时候就会执行,这点我刚开始也和魅惑,导致容易出错,这点我们可以采取一些方法避免执行,比如可以定义一个变量fig=false

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(this.fig)
{
string selectValue = this.cmbAddMember.SelectedValue.ToString(); rtbaddMember.SelectedText = selectValue;
}
}

那么肯定想在加载窗体后,执行了,所以在加载窗体后我们还要把fig的值设为true

private void SetAutoMessage_Load(object sender, EventArgs e)
{
loadCombox();
loadMessageTemplet();
fig= true;
}

转自:https://www.cnblogs.com/masonlu/p/9300241.html

[C#]WinForm 中 comboBox控件之数据绑定的更多相关文章

  1. WinForm 中 comboBox控件之数据绑定

    一.IList 现在我们直接创建一个List集合,然后绑定 1 IList<string> list = new List<string>(); 2 list.Add(&quo ...

  2. winform中comboBox控件加默认选项的问题

    winform程序设计中,label,TextBox,ComboBox等几个控件几乎是用得最多的,在设计中经常会遇到一些小问题,如:comboBox控件绑定了数据源之后,如何设置默认值? combob ...

  3. WinForm 中 comboBox控件数据绑定

    一.IList 现在我们直接创建一个List集合,然后绑定 IList<string> list = new List<string>(); list.Add("11 ...

  4. winform中ComboBox控件的简单使用

    在开发winform中用到了ComboBox,但是发现和asp.net中的DropDownList差别比我想象中的大. 给ComboBox添加数据总结的有两种方法(绑定数据库在这里不说): 第一种方法 ...

  5. Winform中checklistbox控件的常用方法

    Winform中checklistbox控件的常用方法最近用到checklistbox控件,在使用其过程中,收集了其相关的代码段1.添加项checkedListBox1.Items.Add(" ...

  6. Winform中Treeview控件失去焦点,将选择的节点设置为高亮显示 (2012-07-16 13:47:07)转载▼

    Winform中Treeview控件失去焦点,将选择的节点设置为高亮显示 (2012-07-16 13:47:07)转载▼标签: winform treeview drawnode Treeview控 ...

  7. WinForm编程时窗体设计器中ComboBox控件大小的设置

    问题描述: 在VS中的窗体设计器中拖放一个ComboBox控件后想调整控件的大小.发现在控件上用鼠标只能拖动宽度(Width)无法拖动(Height). 解决过程: 1.控件无法拖动,就在属性窗口中设 ...

  8. C#中combobox 控件属性、事件、方法

    一 .combobox 属性.事件.方法公共属性 名称 说明 AccessibilityObject 获取分配给该控件的 AccessibleObject. AccessibleDefaultActi ...

  9. Winform中TextBox控件开启自动提示补全功能

    问题:Winform开发中,有一个TextBox控件用以输入姓名,现希望在输入名字时能够自动提示所有可能的名字. 解答:winform中的TextBox控件含有如下三个属性:   ① AutoComp ...

随机推荐

  1. 实现首字母或拼音检索-sql语句方式

    create function [dbo].[fn_GetPY](@str nvarchar(max),@type int) returns nvarchar(max) as begin ) begi ...

  2. bootstrap validator 出现Maximum call stack size exceeded

    如果用 c# 里面用的是 taghelper 的控件,有可能造成 Maximum call stack size exceeded bootstrap validator  必须是继承  bootst ...

  3. GetSystemInfo 和 GlobalMemoryStatus获取系统信息,内存信息

    // GetSystemInfo.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #in ...

  4. quora 的东西就是不一样

    Coding is just a part of process of problem solving, You should need to understand the underlying pr ...

  5. C#添加文字水印

    使用的是iTextSharp添加PDF水印,由于是接口动态生成PDF,所以采用的是全部是内存流的形式,而且水印是平铺是.iTextSharp版本是5.5 /// <summary> /// ...

  6. 公网定制化yum仓库部署

    公网定制化yum仓库部署 (1)搭建公网源yum仓库 安装wget aliyun源 # wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun ...

  7. Ubuntu16.04上添加用户以及修改用户所属的组

    我的问题是这样的,我的本地的电脑上有一个用户以及一个用户组,我还想添加其他的用户,并且这个用户属于这个已有的用户组 <鸟哥的linux私房菜>针对的是centos系统,还是有一些不一样 实 ...

  8. Windows下 安装Jenkins 并发布至docker 实战

    网上的教程基本都是Linux系统下安装Jenkins,并且发布到Linux系统下的docker中, 于是打算在全部windows的环境中,完成Jenkins的持续集成功能. 环境: 服务器环境: wi ...

  9. Semantic Compositionality through Recursive Matrix-Vector Spaces-paper

    Semantic Compositionality through Recursive Matrix-Vector Spaces 作者信息:Richard Socher Brody Huval Chr ...

  10. 内存溢出eclipse启动tomcat

    1.在eclipse中的Window->preferences->Java->install jar->选择JDK,然后在点击Edit,在Default VM argument ...