2010-09-16 62 views
3

我有一個TextBox,在其中放置一個短語,它是任務的描述或任務的標識。我想用這個TextBox中的文本過濾一個列表。但是當我將文本放入此TextBox時,過濾不起作用,並且DataGridView中的集合不會更改。將過濾器應用於BindingSource,但它不起作用

什麼可能是錯的?

public void BindData() 
{ 
    var emptyBindingSource = new BindingSource(); 
    dataGridViewTaskList.AutoGenerateColumns = false; 
    dataGridViewTaskList.DataSource = emptyBindingSource; 

    var taskList = GetTasks(); 

    _bindingSource = new BindingSource(); 
    _bindingSource.DataSource=taskList.Response; 

    dataGridViewTaskList.AutoGenerateColumns = false; 

    dataGridViewTaskList.DataSource = _bindingSource.DataSource; 

    if (dataGridViewTaskList.Columns["gridViewColumnId"] == null) 
     dataGridViewTaskList.Columns.Add(new DataGridViewColumn() {Name = "gridViewColumnId"}); 
    else 
     dataGridViewTaskList.Columns["gridViewColumnId"].DataPropertyName = "Id"; 

    if (dataGridViewTaskList.Columns["gridViewColumnDescription"] == null) 
     dataGridViewTaskList.Columns.Add(new DataGridViewColumn() {Name = "gridViewColumnDescription"}); 
    else 
     dataGridViewTaskList.Columns["gridViewColumnDescription"].DataPropertyName = "Description"; 
} 

private void tbSearchedPhraseOrId_TextChanged(object sender, EventArgs e) 
{ 
    _bindingSource.Filter = string.Format("Id = '{0}'", tbSearchedPhraseOrId.Text); 
} 

我加入BindData方法如下,它不工作,要麼:

_bindingSource.Filter = string.Format("Id LIKE '%{0}%'", "23"); 

設計師:

this.dataGridViewTaskList.AllowUserToAddRows = false; 
this.dataGridViewTaskList.AllowUserToDeleteRows = false; 
this.dataGridViewTaskList.AllowUserToOrderColumns = true; 
this.dataGridViewTaskList.AllowUserToResizeRows = false; 
this.dataGridViewTaskList.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
         | System.Windows.Forms.AnchorStyles.Left) 
         | System.Windows.Forms.AnchorStyles.Right))); 
this.dataGridViewTaskList.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill; 
this.dataGridViewTaskList.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; 
this.dataGridViewTaskList.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; 
this.dataGridViewTaskList.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { 
this.gridViewColumnId, 
this.gridViewColumnDescription}); 
this.dataGridViewTaskList.Location = new System.Drawing.Point(6, 62); 
this.dataGridViewTaskList.MultiSelect = false; 
this.dataGridViewTaskList.Name = "dataGridViewTaskList"; 
this.dataGridViewTaskList.ReadOnly = true; 
this.dataGridViewTaskList.RowHeadersVisible = false; 
this.dataGridViewTaskList.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect; 
this.dataGridViewTaskList.Size = new System.Drawing.Size(414, 488); 
this.dataGridViewTaskList.TabIndex = 0; 

回答

6

按照documentation,你的基礎數據源(即您的任務列表)必須實現IBindingListView接口有一個工作的Filter屬性。你確定現在是這種情況嗎?

(順便說一句,你應該將DataGridView的DataSource屬性設置爲BindingSource的對象本身,而不是BindingSource.DataSource屬性。)

+0

你有正確的。因爲BindingSource的DataSource被設置爲array:/ – user278618 2010-09-24 07:01:03

+0

@ user278618請問您是怎麼​​做到的? – Volatil3 2013-05-05 20:48:56

0

你嘗試:

_bindingSource.Filter = string.Format("gridViewColumnId = '{0}'", tbSearchedPhraseOrId.Text); 

你可以定義taskList結構嗎?

+0

我沒有任何結果都試過:/ – user278618 2010-09-17 10:11:34

+0

我加入設計師代碼 – user278618 2010-09-17 14:16:10

0

嘗試撥打電話:_bindingSource.ResetBindings(false);設置過濾器後。你

也可以嘗試撥打:

dataGridViewTaskList.ResetBindings(); 
dataGridViewTaskList.Refresh(); 
dataGridViewTaskList.Update(); 
+0

它仍然無法正常工作 – user278618 2010-09-17 16:35:02

1

你應該改變:

dataGridViewTaskList.DataSource = _bindingSource.DataSource; 

dataGridViewTaskList.DataSource = _bindingSource; 

通過改變_bindingSource.Filter你並沒有真正改變_bindingSource.DataSource - 它保持不變,因爲那dataGridViewTaskList.DataSource也不會改變。另一方面,_bindingSource已更改,您可以直接綁定到它以獲取該更改。

1

您可以隨時檢查_bindingSource.SupportsFiltering ,看是否BindingSource的類型支持過濾