如何combobox 数据绑定??

如何combobox 数据绑定??,第1张

combobox.Items.Clear()

dtTable = new DataTable()

strSQL = "select 栏位 from 表"

strResult = DBLink.executeQuery(strSQL, dtTable)

if (!strResult.Equals(""))

{

MessageBox.Show("查询时发生异常!")

}

else

for (int i = 0i <= dtTable.Rows.Count - 1i++)

combobox.Items.Add(dtTable.Rows[i][0].ToString())

dtTable.Dispose()

dtTable = null。

WPF中提供了数据绑定的功能, *** 作起来很方便,集合类的控件几乎都可以用数据源来进行数据的绑定,下面 *** 作一下下拉列表框控件ComboBox控件的数据绑定 *** 作。

要绑定到ComboBox控件的自定义类:

public class LocationRoad

{

public int ID { setget}

public string Code { setget}

public string Info { setget}

}

建立数据源,就将此数据集合当作数据源绑定到ComboBox:

///

/// 当ComboBox选中项更改时发生

///

private LocationRoad _selectLocation

public LocationRoad SelectLocation

{

get

{

return this._selectLocation

}

set

{

this._selectLocation = value

if (this.PropertyChanged != null)

PropertyChanged(this, new PropertyChangedEventArgs("SelectLocation"))

}

}

private ObservableCollection _locationRoad = null

public ObservableCollection LocationSource

{

get

{

if (this._locationRoad == null)

{

this._locationRoad = new ObservableCollection() {

new LocationRoad() { ID = 1, Code = "NGQ", Info = "南岗区" },

new LocationRoad() { ID = 2, Code = "DLQ", Info = "道里区" },

new LocationRoad() { ID = 3, Code = "DWQ", Info = "道外区" },

new LocationRoad() { ID = 4, Code = "PFQ", Info = "平房区" },

new LocationRoad() { ID = 5, Code = "XFQ", Info = "香坊区" },

}

}

return this._locationRoad

}

set

{

this._locationRoad = value

if (this.PropertyChanged != null)

PropertyChanged(this, new PropertyChangedEventArgs("LocationSource"))

}

}

那么,在无DataSource属性的情况下,呢?在Dev 控件方面有多年研究和使用经验的慧都科技,将与你分享对ComboBoxEdit控件进行数据绑定的心得。虽然没有DataSource属性,但是ComboBoxEdit控件的comboBoxEdit1.Properties.Items.Add(object item)方法足以实现数据绑定.1.先用GetAreaDataTable方法返回一个DataTable,代码如下:public static DataTable GetAreaDataTable(){string sqlStr = "select BMMC from usiCLB where BMLID=3"DataBase db = new DataBase()DataTable dt = db.GetDataTable(sqlStr)return dt}2.然后将数据循环添加到ComboBoxEdit中,代码如下:private void LoadComboBoxEdit(){this.comboBoxEdit1.Properties.NullText = "请选择..."DataTable dt = UserInfo.GetAreaDataTable()for (int i = 0i <dt.Rows.Counti++){comboBoxEdit1.Properties.Items.Add(dt.Rows[i]["BMMC"].ToString())}}3.效果图:


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6621560.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-25
下一篇 2023-03-25

发表评论

登录后才能评论

评论列表(0条)

保存