阅读: 120 评论: 0 做者: blackcore 发表于 2009-11-20 14:44 原文连接html
1. DataTablewindows
用DataTable直接绑定,只须要设置DataSource、DisplayMember、ValueMember三个属性便可。ide
Code
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绑定后默认显示第一项,但须要一项提示性选项,我没有找到什么好方法实现了。this
上网看一些人用ComboBox.SelectedIndex = -1或设置ComboBox.Text或初始化设置ComboBox.Items一个项为初始项或设置ComboBox.DropDownStyle,但我这里都没达到效果。spa
本应实现效果A,但以上只能实现B效果,因此以上不符合要求。.net
效果A
效果B
code
2. ComboBox.Items.Addorm
一开始使用时,觉得像Asp.net那样有ListItem属性可使用,但Items只有几个特别简单的属性,还好Add(object item),因此就只能在object这里做文章了。htm
因此就把要绑定的item新new 了一个对象,再重写ToString(),如是乎就能够了。
由于在整个页面中,有不少相似的ComboBox控件,因此就小小的抽象了一下,而后就能够便捷的实现效果B了。具体实现方式以下:
Code
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);
}
}
实现抽象便可
Code
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的绑定
Code
DataBindControlsImplement implement = new BlackCore.App.Method.DataBindControlsImplement();
implement.dataBindComboBox(this.searchCmbConsumeMarket, true, bllMarket.GetList("").Tables[0], "Name", "ID");
ComboBox的获取
Code
if (StringTools.ObjectToInt(searchCmbConsumeMarket.SelectedIndex) != 0)
{
DataBindControlsImplement.ComboBoxItemTextValue comboItem =
(DataBindControlsImplement.ComboBoxItemTextValue)this.searchCmbConsumeProject.SelectedItem;
string selectedText = comboItem.selectText;
int selectedValue = comboItem.selectValue;
}
本人初学WinForm开发,以上内容是我的整理以便有需而用,其如有误,烦请帮忙指点更正,衷心感谢!BlackCore敬上!
发表评论
新闻频道:MyLatch:社交化的求职网站
推荐连接:Windows 7专题发布
网站导航:博客园首页 我的主页 新闻 社区 博问 闪存 知识库