2012-08-09 115 views
0

我正在使用.net 2.0的Windows应用程序。 UI应用程序有一个数据网格,数据将从XML文件填充。 数据网格有500多行。排序功能已经实现。但客户仍然希望在其中一个列上有查找选项或搜索功能,并在文本框中输入前3个字母,并且必须在网格中进行搜索并显示以搜索开始的相关行标准。在C#窗口的datagrid列上实现搜索功能appl

任何建议,请如何实现这一点.... 感谢

回答

0

您可以使用BindingSource的对象过滤选项。

private BindingSource dashBoardBindingSource = new BindingSource(); 

dashBoardBindingSource.DataSource=<<data source items>>; 
dashBoardBindingSource.Filter="Column Name=textbox.text"; 
datagrid.DataSource = dashBoardBindingSource; 
0

收集完整的数据集合,然后当需要执行过滤器时,创建过滤的集合并将过滤的集合绑定到网格。只需将适当的文本更改事件连接到您的过滤器框,即可调用FilterGridData。当通过多列进行过滤时,它也可以很好地工作。哦,你不必在这里使用BindingList。使用任何你想绑定到网格的数据源 - 这个核心就是“通过使用LINQ进行过滤来创建过滤的集合。”

BindingList<Foo> _allFoos; 

    private void LoadData(IEnumerable<Foo> dataToDisplayInGrid) 
    { 
     this._allFoos = new BindingList<Foo>(dataToDisplayInGrid.ToList()); 
     this.FilterGridData(string.Empty); 
    } 

    private void FilterGridData(string filterText) 
    { 
     BindingList<Foo> filteredList = null; 
     if (!string.IsNullOrEmpty(filterText)) 
     { 
      string lowerCaseFilterText = filterText.ToLower(); 
      IList<Foo> filteredItems = this._allFoos.Where(x => (x.Name ?? string.Empty).ToLower().Contains(lowerCaseFilterText)).ToList(); 
      filteredList = new BindingList<Foo>(filteredItems); 
     } 
     else 
     { 
      filteredList = new BindingList<Foo>(this._allFoos); 
     } 

     dataGrid.DataSource = filteredList; 
    } 
+0

我喜欢这个过滤器,但在.Net 2.0框架中没有LINQ? – 2013-08-07 19:16:16