2012-08-09 117 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