4

我有一个文本框,我在其中放置了一个短语,它可以是任务的描述,也可以是任务的 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;
4

6 回答 6

7

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

(顺便说一句,您应该将 DataGridView 的 DataSource 属性设置为 BindingSource 对象本身,而不是 BindingSource.DataSource 属性。)

于 2010-09-19T20:06:49.580 回答
3

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

于 2017-08-20T17:41:28.130 回答
1

你应该改变:

dataGridViewTaskList.DataSource = _bindingSource.DataSource;

dataGridViewTaskList.DataSource = _bindingSource;

通过改变_bindingSource.Filter你实际上并没有改变_bindingSource.DataSource——它保持不变,因此dataGridViewTaskList.DataSource也不会改变。另一方面,_bindingSource被改变了,你可以直接绑定到它来获得那个改变。

于 2010-09-24T09:35:41.297 回答
0

您是否尝试过:

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

你能定义taskList结构吗?

于 2010-09-16T13:50:23.907 回答
0

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

您也可以尝试致电:

dataGridViewTaskList.ResetBindings();
dataGridViewTaskList.Refresh();
dataGridViewTaskList.Update();
于 2010-09-17T13:23:55.190 回答
0

替代 IEnumerable, List.....等全局变量

listBindingSource.DataSource = List();

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

于 2018-07-09T06:19:25.493 回答