WinForm 中ComboBox 绑定总结
1. DataTable 
this.cmbConsumeSuperMarket.DataSource = dtSuperMarket;
this.cmbConsumeSuperMarket.DisplayMember = "Name"; this.cmbConsumeSuperMarket.ValueMember = "ID"; this.cmbConsumeSuperMarket.SelectedIndex = 0;

using System.Data;
using System.Windows.Forms;
namespace BlackCore.App.Method
{
//抽象类 DataBindControls 引入抽象方法 dataBindComboBox(……)
public abstract class DataBindControls
{
///
/// 绑定 ComboBox
///
/// ComboBox Control
/// 是否为此控件插入一个默认选项且默认选中
/// 需要绑定的DataTable
/// 显示文字(DisplayMember)
/// ValueMember
public abstract void dataBindComboBox(ComboBox cmb, bool isInsertDefaultItem, DataTable dt, string selectedText, string selectedValue);
}
} 
using System.Data;
using System.Windows.Forms;
using BlackCore.FinancialLibrary;
namespace BlackCore.App.Method
{
//实现抽象
//类 DataBindControlsImplement 重 写 dataBindComboBox,并提供一个具体实现。
//由 于 DataBindControlsImplement 中没有了抽象成员,因此可以(但并非必须) 将 DataBindControlsImplement 声明为非抽象类。
public class DataBindControlsImplement : DataBindControls
{
public override void dataBindComboBox(ComboBox comboBox, bool isInsertDefaultItem, DataTable dataTable, string selectedText, string selectedValue)
{
if (dataTable != null && dataTable.Rows != null && dataTable.Rows.Count > 0)
{
if (comboBox.Items.Count > 0)
{
comboBox.Items.Clear();
}
int i = 1;
foreach (DataRow dataRow in dataTable.Rows)
{
//comboBox.SelectedText = StringTools.ObjectToStr(dataRow[selectedText]).Trim ();
//comboBox.SelectedValue = StringTools.ObjectToInt(dataRow[selectedValue]).ToString ();
//BlackCore.BLL.FinancialManage.FMProject bllProject = new BlackCore.BLL.FinancialManage.FMProject();
//BlackCore.Model.FinancialManage.FMProject modelProject = new BlackCore.Model.FinancialManage.FMProject();
//modelProject = bllProject.GetModel(StringTools.ObjectToInt(dataRow["ID"]));
//用如下这种方式就只有selectedText,而没有selectedValue
//comboBox.Items.Add(StringTools.ObjectToStr(dataRow[selectedText]).Trim());
//可以存储在ComboBox中的任何种类的对象,而不是字符串。重写toString()方法生成的文本框将显示。
//这样就可以实现selectedText,selectedValue或更多需要的属性
comboBox.Items.Add(new ComboBoxItemTextValue(StringTools.ObjectToInt(dataRow[selectedValue]).ToString(), StringTools.ObjectToStr(dataRow[selectedText])));
}
if (isInsertDefaultItem)
{
comboBox.Items.Insert(0, "请选择
");
}
comboBox.SelectedIndex = 0;
}
}
public class ComboBoxItemTextValue
{
public string selectText;
public string selectValue;
public ComboBoxItemTextValue(string _selectValue, string _selectText)
{
selectValue = _selectValue;
selectText = _selectText;
}
public override string ToString()
{
return selectText;
}
}
}
}

DataBindControlsImplement implement = new BlackCore.App.Method.DataBindControlsImplement();
implement.dataBindComboBox(this.searchCmbConsumeMarket, true, bllMarket.GetList("").Tables[0], "Name", "ID"); 
if (StringTools.ObjectToInt(searchCmbConsumeMarket.SelectedIndex) != 0)
{
DataBindControlsImplement.ComboBoxItemTextValue comboItem =
(DataBindControlsImplement.ComboBoxItemTextValue)this.searchCmbConsumeProject.SelectedItem;
string selectedText = comboItem.selectText;
int selectedValue = comboItem.selectValue;
}
用DataTable直接绑定,只需 要设置DataSource、DisplayMember、ValueMember三个属性即可。

this.cmbConsumeSuperMarket.DataSource = dtSuperMarket;
this.cmbConsumeSuperMarket.DisplayMember = "Name"; this.cmbConsumeSuperMarket.ValueMember = "ID"; this.cmbConsumeSuperMarket.SelectedIndex = 0;
在使用时使用如下方式,即可取得相应 的ID和Name,这样就可以基本满足业务要求了。
StringTools.ObjectToInt(this.cmbConsumeSuperMarket.SelectedValue);
StringTools.ObjectToStr(this.cmbConsumeSuperMarket.SelectedText);
StringTools.ObjectToStr(this.cmbConsumeSuperMarket.SelectedText);
但如上的问题是,因为ComboBox绑定后默认显示第一项,但需要一项提示性选项,我没有找到什么好方法实现了。
上网看一些人用ComboBox.SelectedIndex = -1或设置ComboBox.Text或初始化设置ComboBox.Items一个项为初始项或设置ComboBox.DropDownStyle,但 我这里都没达到效果。
本应实现效果A,但以上只能实现B效果,所以以上不符合要求。
效果A 效果B
2. ComboBox.Items.Add
一开始使用时,以为像Asp.net那样有ListItem属性可以使用,但Items只有几个特别简单的属性,还好Add(object item),所以就只能在object这里作文章了。
所以就把要绑定的item新new 了一个对象,再重写ToString(),如是乎就可以了。
因为在整个页面中,有很多类似的ComboBox控件,所以就小小的抽象了一下,然后就可以便捷的实现效果B了。具体实现方式如下:

using System.Data;
using System.Windows.Forms;
namespace BlackCore.App.Method
{
//抽象类 DataBindControls 引入抽象方法 dataBindComboBox(……)
public abstract class DataBindControls
{
///
/// 绑定 ComboBox
///
/// ComboBox Control
/// 是否为此控件插入一个默认选项且默认选中
/// 需要绑定的DataTable
/// 显示文字(DisplayMember)
/// ValueMember
public abstract void dataBindComboBox(ComboBox cmb, bool isInsertDefaultItem, DataTable dt, string selectedText, string selectedValue);
}
}
实现抽象即可

using System.Data;
using System.Windows.Forms;
using BlackCore.FinancialLibrary;
namespace BlackCore.App.Method
{
//实现抽象
//类 DataBindControlsImplement 重 写 dataBindComboBox,并提供一个具体实现。
//由 于 DataBindControlsImplement 中没有了抽象成员,因此可以(但并非必须) 将 DataBindControlsImplement 声明为非抽象类。
public class DataBindControlsImplement : DataBindControls
{
public override void dataBindComboBox(ComboBox comboBox, bool isInsertDefaultItem, DataTable dataTable, string selectedText, string selectedValue)
{
if (dataTable != null && dataTable.Rows != null && dataTable.Rows.Count > 0)
{
if (comboBox.Items.Count > 0)
{
comboBox.Items.Clear();
}
int i = 1;
foreach (DataRow dataRow in dataTable.Rows)
{
//comboBox.SelectedText = StringTools.ObjectToStr(dataRow[selectedText]).Trim ();
//comboBox.SelectedValue = StringTools.ObjectToInt(dataRow[selectedValue]).ToString ();
//BlackCore.BLL.FinancialManage.FMProject bllProject = new BlackCore.BLL.FinancialManage.FMProject();
//BlackCore.Model.FinancialManage.FMProject modelProject = new BlackCore.Model.FinancialManage.FMProject();
//modelProject = bllProject.GetModel(StringTools.ObjectToInt(dataRow["ID"]));
//用如下这种方式就只有selectedText,而没有selectedValue
//comboBox.Items.Add(StringTools.ObjectToStr(dataRow[selectedText]).Trim());
//可以存储在ComboBox中的任何种类的对象,而不是字符串。重写toString()方法生成的文本框将显示。
//这样就可以实现selectedText,selectedValue或更多需要的属性
comboBox.Items.Add(new ComboBoxItemTextValue(StringTools.ObjectToInt(dataRow[selectedValue]).ToString(), StringTools.ObjectToStr(dataRow[selectedText])));
}
if (isInsertDefaultItem)
{
comboBox.Items.Insert(0, "请选择

}
comboBox.SelectedIndex = 0;
}
}
public class ComboBoxItemTextValue
{
public string selectText;
public string selectValue;
public ComboBoxItemTextValue(string _selectValue, string _selectText)
{
selectValue = _selectValue;
selectText = _selectText;
}
public override string ToString()
{
return selectText;
}
}
}
}
ComboBox的绑定

DataBindControlsImplement implement = new BlackCore.App.Method.DataBindControlsImplement();
implement.dataBindComboBox(this.searchCmbConsumeMarket, true, bllMarket.GetList("").Tables[0], "Name", "ID");
ComboBox的获取

if (StringTools.ObjectToInt(searchCmbConsumeMarket.SelectedIndex) != 0)
{
DataBindControlsImplement.ComboBoxItemTextValue comboItem =
(DataBindControlsImplement.ComboBoxItemTextValue)this.searchCmbConsumeProject.SelectedItem;
string selectedText = comboItem.selectText;
int selectedValue = comboItem.selectValue;
}
分类:
WinForm开发
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架