首页 [讲稿]WinForm中ComboBox绑定总结

[讲稿]WinForm中ComboBox绑定总结

举报
开通vip

[讲稿]WinForm中ComboBox绑定总结[讲稿]WinForm中ComboBox绑定总结 WinForm中ComboBox绑定总结 WinForm 中ComboBox 绑定总结 1. DataTable 用DataTable直接绑定,只需 要设置DataSource、DisplayMember、 ValueMember三个属性即可。 this.cmbConsumeSuperMarket.DataSource = dtSuperMarket; this.cmbConsumeSuperMarket.DisplayMember = "Name"; th...

[讲稿]WinForm中ComboBox绑定总结
[讲稿]WinForm中ComboBox绑定 总结 初级经济法重点总结下载党员个人总结TXt高中句型全总结.doc高中句型全总结.doc理论力学知识点总结pdf WinForm中ComboBox绑定总结 WinForm 中ComboBox 绑定总结 1. DataTable 用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); 但如上的问题是,因为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[select edText]).Trim()); //可以存储在ComboBox中的任何种类的对 象,而不是字符串。重写toString()方法生成的文本框将显示。 //这样就可以实现selectedText, selectedValue或更多需要的属性 comboBox.Items.Add(new ComboBoxItemTextValue(StringTools.ObjectToInt(dataRow[selec tedValue]).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.searc hCmbConsumeProject.SelectedItem; string selectedText = comboItem.selectText; int selectedValue = comboItem.selectValue; } 分类: WinForm开发 绿色通道: 好文要顶 关注我 收藏该文与我联系 风雨者2 关注 - 168 粉丝 - 194 +加关注 0 0 (请您对文章做出评价) ? 上一篇:C# WinForm开发系列 - 文章索引 ? 下一篇:(收藏)《博客园精华集》WebService筛选结果(共79篇) posted on 2010-04-13 11:31 风雨者2 阅读(2945) 评论(4) 编辑 收藏 评论 #1楼 实际上可以在DataTable中先插入一行,再将这个Combobox的 datasouce设为这个DataTable啊 支持(0)反对(0) 2010-04-14 20:04 | qixin6618 #2楼 引用 qixin6618:实际上可以在DataTable中先插入一行,再将这个Combobox的datasouce设为这个DataTable啊 同意 支持(0)反对(0) 2010-04-19 16:10 | jhkmnm #3楼 完全同意,解决我一大问题。谢谢1楼 支持(0)反对(0) 2010-05-10 17:28 | 袁克雄 #4楼[楼主] 1. ArrayList al1 = new ArrayList(); 2. al1.Add(new DictionaryEntry("Y", "军品")); 3. al1.Add(new DictionaryEntry("N", "民品")); 4. cbIsArmy.DataSource = al1; 5. cbIsArmy.DisplayMember = "Value"; 6. cbIsArmy.ValueMember = "Key";
本文档为【[讲稿]WinForm中ComboBox绑定总结】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_633808
暂无简介~
格式:doc
大小:23KB
软件:Word
页数:0
分类:高中语文
上传时间:2017-09-18
浏览量:9