WinForm实现为ComboBox绑定数据源并提供下拉提示功能
作者:shichen2014 时间:2021-12-16 11:00:38
本文实例展示了WinForm实现为ComboBox绑定数据源并提供下拉提示功能,这是一个非常有实用价值的功能,具体实现方法如下:
主要功能代码如下:
/// <summary>
/// 为ComboBox绑定数据源并提供下拉提示
/// </summary>
/// <typeparam name="T">泛型</typeparam>
/// <param name="combox">ComboBox</param>
/// <param name="list">数据源</param>
/// <param name="displayMember">显示字段</param>
/// <param name="valueMember">隐式字段</param>
/// <param name="displayText">下拉提示文字</param>
public static void Bind<T>(this ComboBox combox, IList<T> list, string displayMember, string valueMember, string displayText)
{
AddItem(list, displayMember, displayText);
combox.DataSource = list;
combox.DisplayMember = displayMember;
if (!string.IsNullOrEmpty(valueMember))
combox.ValueMember = valueMember;
}
private static void AddItem<T>(IList<T> list, string displayMember, string displayText)
{
Object _obj = Activator.CreateInstance<T>();
Type _type = _obj.GetType();
if (!string.IsNullOrEmpty(displayMember))
{
PropertyInfo _displayProperty = _type.GetProperty(displayMember);
_displayProperty.SetValue(_obj, displayText, null);
}
list.Insert(0, (T)_obj);
}
使用示例:
List<CommonEntity> Sources = new List<CommonEntity>();
private void WinComboBoxToolV2Test_Load(object sender, EventArgs e)
{
CreateBindSource(5);
comboBox1.Bind(Sources, "Name", "Age", "--请选择--");
}
private void CreateBindSource(int count)
{
for (int i = 0; i < count; i++)
{
CommonEntity _entity = new CommonEntity();
_entity.Age = i;
_entity.Name = string.Format("Yan{0}", i);
Sources.Add(_entity);
}
}
代码运行效果如下:
标签:WinForm,ComboBox
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅谈spring中scope作用域
2022-08-18 10:42:53
eclipse springboot工程打war包方法及再Tomcat中运行的方法
2023-04-14 09:21:40
![](https://img.aspxhome.com/file/2023/9/78349_0s.png)
使用Nexus搭建Maven私服教程的方法步骤
2023-03-05 18:56:58
![](https://img.aspxhome.com/file/2023/3/66203_0s.jpg)
Java servlet后端开发超详细教程
2022-11-01 06:13:50
![](https://img.aspxhome.com/file/2023/1/113471_0s.png)
Java使用线程同步解决线程安全问题详解
2022-02-28 02:03:24
![](https://img.aspxhome.com/file/2023/9/78029_0s.png)
jdk自带线程池实例详解
2022-05-14 02:04:56
![](https://img.aspxhome.com/file/2023/9/113499_0s.jpg)
Springboot基础学习之初识SpringBoot
2022-08-18 08:48:59
![](https://img.aspxhome.com/file/2023/8/75408_0s.png)
解读@RequestBody与post请求的关系
2022-10-07 02:02:51
![](https://img.aspxhome.com/file/2023/7/95037_0s.png)
IDEA教程之Activiti插件图文详解
2023-11-14 23:06:45
![](https://img.aspxhome.com/file/2023/6/59726_0s.png)
C#判断系统是32位还是64位的方法
2022-04-01 12:44:04
![](https://img.aspxhome.com/file/2023/9/108799_0s.png)
mybatis 对于生成的sql语句 自动加上单引号的情况详解
2023-09-21 15:25:00
![](https://img.aspxhome.com/file/2023/2/61342_0s.jpg)
C++内存对齐的实现
2023-11-22 00:42:20
C#设计模式之简单工厂模式
2023-10-19 21:57:30
![](https://img.aspxhome.com/file/2023/1/106341_0s.jpg)
spring mvc中直接注入的HttpServletRequst安全吗
2021-12-29 07:48:16
![](https://img.aspxhome.com/file/2023/6/63506_0s.png)
Java中try catch处理异常示例
2023-11-07 00:43:17
C#使用NPOI将excel导入到list的方法
2023-11-17 22:49:09
![](https://img.aspxhome.com/file/2023/6/100116_0s.jpg)
SpringBoot项目集成Flyway进行数据库版本控制的详细教程
2023-11-24 05:20:33
![](https://img.aspxhome.com/file/2023/0/59390_0s.png)
Java实现简易Web服务器
2023-11-17 09:10:11
Android 8.0如何完美适配全局dialog悬浮窗弹出
2023-04-09 12:54:00
![](https://img.aspxhome.com/file/2023/8/90138_0s.png)
Java多线程状态及方法实例解析
2021-09-10 22:49:53
![](https://img.aspxhome.com/file/2023/7/74777_0s.png)