一个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;

附上代码 下载

posted on 2012-12-05 22:07  柏柏  阅读(1786)  评论(15编辑  收藏  举报