WinForm 中ComboBox 绑定总结

 1.  DataTable

用DataTable直接绑定,只需要设置DataSource、DisplayMember、ValueMember三个属性即可。

ContractedBlock.gif ExpandedBlockStart.gif 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绑定后默认显示第一项,但需要一项提示性选项,我没有找到什么好方法实现了。

上网看一些人用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了。具体实现方式如下:

ContractedBlock.gif ExpandedBlockStart.gif Code
using System.Data;
using System.Windows.Forms;

namespace BlackCore.App.Method
{
    
//抽象类 DataBindControls 引入抽象方法 dataBindComboBox(……)
    public abstract class DataBindControls
    {
        
/// <summary>
        
/// 绑定ComboBox
        
/// </summary>
        
/// <param name="cmb">ComboBox Control</param>
        
/// <param name="isInsertDefaultItem">是否为此控件插入一个默认选项且默认选中</param>
        
/// <param name="dt">需要绑定的DataTable</param>
        
/// <param name="selectedText">显示文字(DisplayMember)</param>
        
/// <param name="selectedValue">ValueMember</param>
        public abstract void dataBindComboBox(ComboBox cmb, bool isInsertDefaultItem, DataTable dt, string selectedText, string selectedValue);
    }
}

实现抽象即可 

ContractedBlock.gif ExpandedBlockStart.gif 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的绑定

ContractedBlock.gif ExpandedBlockStart.gif Code
DataBindControlsImplement implement = new BlackCore.App.Method.DataBindControlsImplement();

implement.dataBindComboBox(
this.searchCmbConsumeMarket, true, bllMarket.GetList("").Tables[0], "Name""ID");

ComboBox的获取 

ContractedBlock.gif ExpandedBlockStart.gif 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敬上!

 

 

转载于:https://www.cnblogs.com/blackcore/archive/2009/11/20/1606896.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值