一個winform帶自動完成功能的TextBox,效果如下圖,當TextBox輸入字符時,按文本框中的內容查找數據,並綁定在下拉的DataGridView
使用方法如下,控件數據源為List<T>
private void FrmMaterialsRequisitionBill_Load(object sender, EventArgs e)//窗體加載
{
this.txtMaterialType.Mapping.Add("編碼", "TypeNO");//編碼為DataGridView要顯示的列名,"TypeNO"綁定的字段。
this.txtMaterialType.Mapping.Add("類型名稱", "TypeName");//如有多列使用Mapping.Add();即可
this.txtMaterialType.Mapping.Add("拼音碼", "PinyinCode");
this.txtMaterialType.Mapping.Add("備注", "Remark");
this.txtMaterialType.DisplayMember = "TypeName";//DisplayMember指定在TextBox上顯示的字段值
//文本值改變時
this.txtMaterialType.PropertyChanged += new PropertyChangedEventHandler(RequisitionType_PropertyChanged);
}
//數據綁定
public void RequisitionType_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
string requisitionType= txtMaterialType.DisplayValue;//取出TextBox顯示的本文值
this.txtMaterialType.DataSource = requisitionTypeDAL.GetList(temp.TypeName.Contains(requisitionType)).ToList();// 模擬在數據庫中查找數據,然后通過DataSource 綁定
}
//取值,控件中保存的是List<T>的泛型對象,通過Target取值,MaterialsRequisitionType為泛型中的實際對象
MaterialsRequisitionType requisitionType = txtMaterialsRequisitionType.Target as MaterialsRequisitionType;
附上代碼 下載

