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

发布于 2024-09-19 20:16:13 字数 2937 浏览 7 评论 0原文

我有一个文本框,在其中放置了一个短语,它可以是任务的描述,也可以是任务的 ID。我想使用此文本框中的文本来过滤列表。但是,当我将文本放入此 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;

I have a TextBox, in which I put a phrase, which is either the description of a task or the id of a task. I want to filter a list using the text from this TextBox. But when I put text into this TextBox, filtering doesn't work, and the collection in the DataGridView doesn't change.

What can be wrong?

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);
}

I added the following in BindData method and it doesn't work either:

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

Designer:

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

╰つ倒转 2024-09-26 20:16:14

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

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

According to the documentation, your underlying data source (i.e. your task list) must implement the IBindingListView interface to have a working Filter property. Are you sure this is the case right now?

(As an aside, you should set the DataSource property of your DataGridView to the BindingSource object itself rather than the BindingSource.DataSource property.)

ヅ她的身影、若隐若现 2024-09-26 20:16:14

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

You can always check _bindingSource.SupportsFiltering
to see if the BindingSource type supports filtering

顾忌 2024-09-26 20:16:14

您应该更改:

dataGridViewTaskList.DataSource = _bindingSource.DataSource;

通过

dataGridViewTaskList.DataSource = _bindingSource;

更改 _bindingSource.Filter 您实际上并没有更改 _bindingSource.DataSource - 它保持不变,并且由于 dataGridViewTaskList.DataSource code> 也没有改变。另一方面,_bindingSource 已更改,您可以直接绑定到它以获取该更改。

You should change:

dataGridViewTaskList.DataSource = _bindingSource.DataSource;

to

dataGridViewTaskList.DataSource = _bindingSource;

By changing _bindingSource.Filter you're not actualy changing _bindingSource.DataSource - it stays the same and because of that dataGridViewTaskList.DataSource doesn't change either. In the other hand, _bindingSource is changed, and you can bind directly to it to get that change.

若言繁花未落 2024-09-26 20:16:14

您是否尝试过:

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

您可以定义taskList结构吗?

Did you try with:

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

Can you define taskList structure?

拥醉 2024-09-26 20:16:14

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

您也可以尝试拨打:

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

Try to call : _bindingSource.ResetBindings(false); after setting filter.

Also you can try to call:

dataGridViewTaskList.ResetBindings();
dataGridViewTaskList.Refresh();
dataGridViewTaskList.Update();
溺深海 2024-09-26 20:16:14

替代方案
IEnumerable、List...等全局变量

listBindingSource.DataSource = List();

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

Alternative
IEnumerable, List.....etc Global Variable

listBindingSource.DataSource = List();

Then simple filter the list and Re-assign it
listBindingSource.DataSource = List.FindAll(t => t.x == yourValue);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文