winform一个带自动完成功能的TextBox


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

附上代码 下载


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM