WinForm 中ComboBox 绑定总结

http://www.cnblogs.com/blsong/archive/2010/04/13/1710955.html

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[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;
}

原文地址:https://www.cnblogs.com/Echo529/p/6382195.html