将过滤器应用于BindingSource,但它不起作用

时间:2010-09-16 10:16:40

标签: c# winforms .net-3.5 bindingsource

我有一个TextBox,我在其中放了一个短语,它可以是任务的描述,也可以是任务的id。我想使用此TextBox中的文本过滤列表。但是当我将文本放入此TextBox时,过滤不起作用,并且DataGridView中的集合不会更改。

有什么不对?

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 个答案:

答案 0 :(得分:6)

根据documentation,您的基础数据源(即您的任务列表)必须实现IBindingListView接口才能拥有可用的Filter属性。你确定现在就是这种情况吗?

(另外,您应该将DataGridView的DataSource属性设置为BindingSource对象本身,而不是BindingSource.DataSource属性。)

答案 1 :(得分:1)

你应该改变:

dataGridViewTaskList.DataSource = _bindingSource.DataSource;

dataGridViewTaskList.DataSource = _bindingSource;

通过更改_bindingSource.Filter,您实际上并未改变_bindingSource.DataSource - 它保持不变,因此dataGridViewTaskList.DataSource也不会改变。另一方面,_bindingSource已更改,您可以直接绑定它以获得更改。

答案 2 :(得分:1)

您随时可以查看_bindingSource.SupportsFiltering 查看BindingSource类型是否支持过滤

答案 3 :(得分:0)

您尝试过:

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

你能定义taskList结构吗?

答案 4 :(得分:0)

尝试在设置过滤器后调用_bindingSource.ResetBindings(false);

您也可以尝试致电:

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

答案 5 :(得分:0)

替代 IEnumerable,列表.....等全局变量

listBindingSource.DataSource = List();

然后简单过滤列表并重新分配 listBindingSource.DataSource = List.FindAll(t => t.x == yourValue);